Forum Moderators: coopster
I need to match every instance of
:not(whatever) in a string, there could be more than one and whatever can be well whatever I then need to strip the :not() leaving just the "whatever" part and also put spaces between them.
$str = "h2:not(.post):not(FOO)";
$do = preg_match_all("/:not\(([^()\r\n]*)\)/", $str, $matches);// Check if regex was successful
if ($do==true) {
// combine the 2 arrays so first become key and second becomes value
$match = array_combine($matches[0], $matches[1]);// replace key with the value in the string and add a space
foreach ($match as $key=>$value) {
$str = str_replace($key, ' '.$value, $str);
}
}
it works but just want to learn if there's a better way to search and replace multiple strings.
so that "$2" token is that taking the inside of "my" brackets because you've wrapped them in another set of grouping brackets i.e. would "$1" apply to the not part and if so is tokens the right word that I need to learn about
thanks
[edit] thanks very much just the job! - tested and working and much easier :)[/edit]
[edited by: SuzyUK at 3:50 pm (utc) on May 23, 2008]
If you remove the parentheses from the :not then you would use $1 instead of $2. I don't know the exact terminology for that but I'm no expert by any means.
Glad it worked.
$1...$2
As JD Morgan reminded me yesterday, those are called 'back-references' - captured by the brackets within the regex.
Of course, you may have found a workaround to this, I am only responding to what I have inferred from your last comment.