preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

preg_replace

Post by Silver_Eclipse »

does anyone know why preg_replace would return and error like
Warning: Unknown modifier '(' on line 27
but if i change it to ereg_replace or eregi_replace it works fine, and i really need the limit option of preg_replace.
User avatar
9902468
Forum Commoner
Posts: 89
Joined: Thu Jun 06, 2002 6:39 am
Location: Europe

Post by 9902468 »

some of php's functions are compatible with perl like regular expression, and some are not.
Could this be the case?

-9902468
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

uhh i have no idea i dont use perl i just want the function to work arg.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

each preg-expression starts with an delimeter that you may choose freely (quite). common chars are / or !. You mark the end of the expression with the same character (maybe followed by some modifiers like m for multiline).
I assume your expression looks like '\([0-9]*\)' or simliar.
the preg-function starts to parse the string, finds \ as first character, interprets it as delimeter, finds the next occurence and then tries to parse the following chars as modifiers; in your case ), but this isn't a valid modifier.

in simple words:
ereg_xyz('expr') -> preg_xyz('/expr/'); or
ereg_xyz('expr') -> preg_xyz('!expr!'); or
...
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Huh! God you know a lot Volka. I have to keep reading the PHP manual.... :P
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

i'd reccommend using eregi, unless you're doing case sensitive work. ereg will be good for case sensitive searching. preg was mainly there for users coming over from perl and having perl coding habits
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

but preg_ is faster and more flexible
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

eh, i don't think he's making a large scale backend upon which thousands of users will be accessing it at once... right now i was going for the ease over efficiency
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, I don't think putting a char in front and the same char at the end of the string is too difficult ;)
But of course one may use the function one likes
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Regarding the speed of preg_... ereg_... etc I've found this handy table at
http://php.weblogs.com/
"So I just benchmarked the following search methods, searching for my name in a string 6044 bytes long. My name was at the very end of the string to exercise the code. Here's the results for 500 searches on PHP 4.2.1, on a 800 Mhz Windows 2000 server:"
  • $rez = strpos($SS,'John Lim'); ------ 0.0188 seconds
    $rez = strstr($SS,'John Lim'); ------ 0.0294 seconds
    $rez = preg_match('/John Lim/',$SS); ------ 0.0307 seconds
    $rez = ereg('John Lim',$SS); ------ 1.5600 seconds
    $rez = preg_match('/John Lim/i',$SS); ------ 0.0387 seconds
    $rez = preg_match('/[a-z ]+Lim/i',$SS);------ 0.0573 seconds
    $rez = preg_match('/John[ a-z]+/i',$SS)------ 0.0403 seconds
    $rez = eregi('John[ a-z]*',$SS); ------ 2.1200 seconds
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I like PCRE.
Even with the STL wrapper implementation I get more than acceptable response time :D
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

well i need the limit option of preg that is not in ereg or eregi
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok so its working now thnx, but i dont understand the limit thing because my subject is $file[$array]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what do you want to limit?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

the preg_replace has a limit that u can set for the number of matches it replaces, i want to do that but it keeps replacing all the matches for some reason.
Post Reply