Forum Moderators: coopster
pattern ( OR [ OR { any text ); OR ]; OR };
But only when it appears inside <-- --> and only with the entire rest of the pattern. Also, the any text has to be able to handle new lines. Here's an example.
I would like to turn this:
<--
pattern (some text);
pattern [some more text];
pattern {some text
with a new line in it};
pattern don't match this
pattern [or this]
pattern [or this
pattern or this];
-->
pattern (don't match this either);
pattern and definitely not this!
<--
pattern [but you can
match this];
-->
Into
<--
pineapple (some text);
pineapple [some more text];
pineapple {some text
with a new line in it};
pattern don't match this
pattern [or this]
pattern [or this
pattern or this];
-->
pattern (don't match this either);
pattern and definitely not this!
<--
pineapple [but you can
match this];
-->
So far the code I have to this looks like:
<?php
$text='<--
pattern (some text);
pattern [some more text];
pattern {some text
with a new line in it};
pattern dont match this
pattern [or this]
pattern [or this
pattern or this];
-->
pattern (dont match this either);
pattern and definitely not this!
<--
pattern [but you can
match this];
-->';
$output=preg_replace('@(<--.*?)pattern ([(\[{])(.*?)(\);¦];¦};){1}(.*?-->)@s','$1pineapple $2$3$4$5',$text);
echo $output;
?>
But that only changes the first patterns in the first block and the second block. I tried putting the preg_replace in a while loop that went until it didn't match any patterns--which worked until I tried putting it into development where I got some really crazy problems. I actually need this functionality for a more complex project, unfortunately I'm unable to divulge the details of it :(
If anyone has anything that might be able to help I'd really appreciate it! I'm not a regular expressions expert by any means so I'd really love to learn how to do this right.
[edited by: eelixduppy at 11:33 pm (utc) on July 10, 2009]
[edit reason] disabled smileys [/edit]
function callback($matches)
{
$match_string = 'pattern';
$replace_string = 'pineapple';
$pattern = '/'.preg_quote($match_string).'\s([\[{(]([^\[\]{}()](?!'.preg_quote($match_string).'))*[\]})];)/';
return preg_replace($pattern, "$replace_string \\1", $matches[0]);
}
/* Grab all of the text between the two tags <-- and --> */
$pattern = '/<--[^->]*-->/';
echo '<pre>'.preg_replace_callback($pattern, 'callback', $text).'</pre>';
And welcome to WebmasterWorld!