473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

eregi_replace and Regular Expression Quandry

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?
Aug 29 '05 #1
2 1141
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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 31 '05 #2
saiena wrote:
Is there a way in my regular expression syntax to cause alternating
occurences of the search string to be replaced?

$item[content] = eregi_replace($search_string, $replace_string,
$item[content]);


How about using:
$string = preg_replace("/$search_string(.*?($search_string|$))/",
"$replace_string\$1",$string);

where I wrote $string instead of $item[content] cause I didn't
like the looks of that content. This assumes your $search_string
doesn't have unusual characters (in which case you'd need to read up on
regular expressions and escaping characters).

Csaba Gabor from New York

Sep 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David | last post by:
Hi, I've had a search through google but couldn't really find the answer I was looking for.I'm new to PHP, so please take it <relatively> easy. I've created a script which runs some SNMP...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
9
by: Bob | last post by:
Hi, Maybe this problem has already been mentionned, but I am new to this group, so here we go. $string = "This is for ABC only" $new = (eregi_replace('ABC','<b>ABC</b>',$string)); print...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.