Forum Moderators: coopster

Message Too Old, No Replies

Replacing text, preg match, merge array

         

SuzyUK

10:24 pm on May 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I nearly came and asked for help earlier, but I persevered and now I'd like to know, how'd I do.. and is there a more efficient way of doing this?

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.

Murdoch

2:59 pm on May 23, 2008 (gmt 0)

10+ Year Member



How's this?

$str = preg_replace("/(:not)\(([^)]+)\)/"," $2 ",$str);

SuzyUK

3:44 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



murdoch, it certainly looks good, I had a feeling there would be a much shorter way to do it - I don't know enough functions yet :)

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]

Murdoch

3:56 pm on May 23, 2008 (gmt 0)

10+ Year Member



Technically you don't need parentheses around the :not, I just personally do that to separate parts of my regex to make it easier to read for myself.

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.

Receptional Andy

4:57 pm on May 23, 2008 (gmt 0)



$1...$2

As JD Morgan reminded me yesterday, those are called 'back-references' - captured by the brackets within the regex.

SuzyUK

5:21 pm on May 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thank you Andy (and jd), I did a search on tokens and realised it wasn't the right word.

and thanks again Murdoch once I took a look at your regex more in depth I realised another part of it "^)" affected another one I had written so I allowed for that too so fixed two things in one go :)

ta!

Murdoch

7:40 pm on May 23, 2008 (gmt 0)

10+ Year Member



I don't think the regex will work with multiple instances within a string if you don't disallow that end parenthesis. Otherwise it will just remove the :not and the first and last parentheses contained in the string.

Of course, you may have found a workaround to this, I am only responding to what I have inferred from your last comment.

Murdoch

8:51 pm on May 23, 2008 (gmt 0)

10+ Year Member



Hilariously enough, I just used your OP code to create a regex that resizes embedded images on the fly.

So thank YOU.