Horje
What is the RegEx for finding a semi-color between brackets? - Wordpress Solution
[Solved] - What is the RegEx for finding a semi-color between brackets? - Wordpress Solution
Try following codes
Not really a WordPress expression, but surely you PHP geniuses can help. Assume I have text like this:
Hello, how are you? Nice day yes? [[ secret_code(); ]] Jolly, fun ride? 
or:
Hello, how are you? Nice day yes? [[ secret_code() ]] Jolly, fun ride? 
or:
In 1986 IBM recorded the largest profit ever recorded by any corporation in the the history of the world. Nothing would ever be able to compete with IBM. The media was in a swoon about how amazing IBM was. But IBM was already losing ground in the PC market, and they were losing ground in electronics to the Japanese. In [[ find_date_for_quote(); ]] IBM was struggling to avoid bankruptcy.
I need to remove every semi-colon that appears between the double square brackets [[ ]]. What is the regular expression for this? This will be in a script that is looping hundreds of small text snippets like the ones above.

Solution - 1


$new_string = preg_replace("/(\[\[) (\w+)(\(\))(\;) (\]\])/", "$1 $2$3 $5", $string , -1);


Solution - 2

These methods above let you replace one semicolon only. Here is the code to remove all semicolons:

function rw_remove_semicolons($match) {
	$s = $match[0];
	return str_replace(';', '', $s);
}

// test

$s = "In 1986 IBM recorded the largest profit ever recorded by any corporation in the the history of the world. Nothing would ever be able to compete with IBM. The media was in a swoon about how amazing IBM was. But IBM was already losing ground in the PC market, and they were losing ground in electronics to the Japanese. In [[ find_date_for_quote(); ]] IBM was struggling to avoid bankruptcy.";

// usage

$s = preg_replace_callback('#\[\[.*?\]\]#', 'rw_remove_semicolons', $s);

echo $s;


Solution - 3

ex: From : [123]some text [caught] more text [123123] sq() [] you get : [123] [caught] [123123] [] using this reg expression: \[[^\]|^\[]*\] the following will match () \(([^\}]+)\)


Solution - 4

Hi Lawrence, Just replace the match for this:

[;]+(?=[^\[\[]*]])
Edit: removed the double quote on my first answer to match all occurrences of ";" inside the [[]]

Date: 2022-10-21 05:18:39
Category: Wordpress
More for You