Page 1 of 2
preg_replace
Posted: Wed Aug 21, 2002 2:35 am
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.
Posted: Wed Aug 21, 2002 6:06 am
by 9902468
some of php's functions are compatible with perl like regular expression, and some are not.
Could this be the case?
-9902468
Posted: Wed Aug 21, 2002 3:39 pm
by Silver_Eclipse
uhh i have no idea i dont use perl i just want the function to work arg.
Posted: Wed Aug 21, 2002 4:05 pm
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
...
Posted: Wed Aug 21, 2002 4:17 pm
by Takuma
Huh! God you know a lot Volka. I have to keep reading the PHP manual....

Posted: Wed Aug 21, 2002 4:51 pm
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
Posted: Wed Aug 21, 2002 5:58 pm
by volka
but preg_ is faster and more flexible
Posted: Wed Aug 21, 2002 6:18 pm
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
Posted: Thu Aug 22, 2002 12:51 am
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
Posted: Thu Aug 22, 2002 11:44 am
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
Posted: Thu Aug 22, 2002 11:49 am
by volka
I like PCRE.
Even with the STL wrapper implementation I get more than acceptable response time

Posted: Thu Aug 22, 2002 9:48 pm
by Silver_Eclipse
well i need the limit option of preg that is not in ereg or eregi
Posted: Sat Aug 24, 2002 4:16 pm
by Silver_Eclipse
ok so its working now thnx, but i dont understand the limit thing because my subject is $file[$array]
Posted: Sat Aug 24, 2002 4:45 pm
by volka
what do you want to limit?
Posted: Sat Aug 24, 2002 4:59 pm
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.