|
Posted by Andy Hassall on 10/24/69 11:25
On Mon, 29 Aug 2005 20:55:46 GMT, "saiena" <saiena@nospam_thetellingtree.com>
wrote:
>Is there a way in my regular expression syntax to cause alternating
>occurences of the search string to be replaced?
>
>Here's my code:
>
>$item[content] = eregi_replace($search_string, $replace_string,
>$item[content]);
>
>This successfully replaces all occurences of $search_string.
>
>Any way to reaplce every other?
The solutions I can think of all need non-greedy expressions, at which point
you need to use the PCRE (preg_*) functions - which IMHO is worth switching to
anyway unless you're really tied to the more basic ereg syntax.
At which point you can either use a clever expression, or just do it with
preg_replace_callback and a Boolean flag or counter accessed by the callback to
work out whether it's an odd or even call, and so whether to replace or not.
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Navigation:
[Reply to this message]
|