Php preg_replace regex lookahead?

Why not something simple like: $html = preg_replace('/(^&)&(^&)/', '$1&$2', $html) If you want to avoid replacing all ampersands inside Javascript, add lookahead/behind for script tags, or first split the document on script tags, and only run the replace on the non-script parts $html = preg_split('//', $html); foreach ($html as $k => $v) { if ($piece0 == "" . $html$k . ""; } This will need some modification if your script tags have attributes If you're sanitizing user content, then you might be better using the tools that are already available.

See HTML Purifier.

Why not something simple like: $html = preg_replace('/(^&)&(^&)/', '$1&$2', $html); If you want to avoid replacing all ampersands inside Javascript, add lookahead/behind for script tags, or first split the document on script tags, and only run the replace on the non-script parts. $html = preg_split('//', $html); foreach ($html as $k => $v) { if ($piece0 == "" . $html$k .""; } This will need some modification if your script tags have attributes.

If you're sanitizing user content, then you might be better using the tools that are already available. See HTML Purifier.

The $skin var is my parsed html, so I look through my hole html, this will not work in my case unfortunatly. As my above works but I want it simpliere, like replace all & if not within script tags. I know I could simply do a replace in each of my functions, but I rather would only write this once – Basti Oct 21 at 16:48 Also tried it with the script tags, but it looks like I never get it right, either error or it don't replace – Basti Oct 21 at 16:52 Will try the split method, thanks :) – Basti Oct 21 at 17:01.

If you just want to convert '&' in content and avoid tags (ie: attribute values) and avoid script blocks, something like below will work for most occurances. However, it should be noted that attribute values should be converted as well. And doing that requires a lot more work.

Working sample ideone.com/9MhCq S&P &&more; and &some; ¬me; && Ñ? & EOD; $rxent = '(?:&(?:A-Za-z_:\w:. -*|\#(?:0-9+|x0-9a-fA-F+));)'; $rxtag = '*?)+\s* )> .

*? *?)+\s*/? |!(?:DOCTYPE.

*? |--. *?

--) ) ) > '; $rxmain = "~(?xs:((?:$rxtag)+) | ((?! $rxent)&))~"; print "$html\n\n"; $html = preg_replace_callback($rxmain, 'fixamp_cb', $html); print "$html\n"; function fixamp_cb( $matches ) { # Return tags and script blocks unchanged. If (isset($matches1) && $matches1) return $matches1; return '&'; }?

Php > $str = ' Your address is {Address:S111}. Your customer name is {Customer:C111}. Your customer id is {CustomerId:C1112}.

You use laptop brand {Laptops:I4}. Thanks. '; php > echo preg_replace('#\{\w+:C(\d+)\}#', 'func_call(\\1)', $str); Your address is {Address:S111}.

Your customer name is func_call(111). Your customer id is func_call(1112). You use laptop brand {Laptops:I4}.Thanks.

– shesek Sep 6 at 6:19 Thanks mate! – Kevin Joymungol Sep 6 at 6:48.

Try /{\w+:C/ As your regex. . * is greedy, it will consume as much as it can.

This a very weird looking thing to do BTW.

1 for noting the weirdness... – Jack Maney Sep 6 at 6:19.

You need to switch to the non-greedy version of *. Try this: $html = preg_replace("/{. *?

:C/", "func_call(", ' Your address is {Address:S111}. Your customer name is {Customer:C111}. Your customer id is {CustomerId:C1112}.

You use laptop brand {Laptops:I4}. Thanks. ').

Won't the . *? Consume Address:S111}.

Your customer name is {Customer? It's not greedy so it won't consume to the very last :C but it will still start after the first {, no? Or does non-greedy search for the shortest match if there are multiple?

I'm not sure. I thought it just meant it would stop at the first match. – Mark Sep 6 at 6:24 It works okay because it is followed by a colon.

The regex . *? : will consume all characters up until the first colon.

Another way to do this is ^:*:. Does that help? – Ray Toal Sep 6 at 6:31 ^:*: would work, yes.

The other way isn't just followed by a colon though, it's followed by :C, so in a example like {a:B} {a:C} it would start at a and go to :C, I believe. – Mark Sep 6 at 15:53.

$html = preg_replace("/\{(^:+)\:(^\}+)\}/", "func_call(\$2)", ' Your address is {Address:S111}. Your customer name is {Customer:C111}. Your customer id is {CustomerId:C1112}.

You use laptop brand {Laptops:I4}. Thanks. '); "search for {, followed by anything but :, remember it in $1, then match the first : followed by anything but }, remember it in $2.

Replace the while match with func_call($2).

Thanks for the tip. Was quite useful – Kevin Joymungol Sep 6 at 6:50.

If you wish you can do this without preg_replace something like this $string =" Your address is %s. Your customer name is %s. Your customer id is %s.

You use laptop brand %s. Thanks. '"; $string = sprintf($string,'{Address:S111}','{Customer:C111}','{CustomerId:C1112}','{Laptops:I4}').

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions