[thelist] PHP Regular Expression Dilemma
Jay Greenspan
jay at trans-city.com
Tue Mar 6 10:20:10 CST 2001
on 3/6/01 11:04 AM, Marty Landman at marty at face2interface.com wrote:
> At Tuesday 3/6/01 10:30 AM, you wrote:
>
>> $Description =
>> eregi_replace("<a[^<]*javascript:[^>]*>([^<]*)</a>","\\1",$Description);
>>
>> The problem is the ([^<]*) part - I need to make this say "any text until
>> you reach the next "</a>" statement.
>
> try ".*?" instead... also I'm more familiar with regex' in perl and wonder
> if you need the "s" option at the end to treat $Description as a single
> line, i.e. ignore newlines.
>
> in perl I might say something more like
>
> $Description =~ /<a[^<]*javascript:.*?>(.*)?</a>/si;
>
The problem is that .*? doesn't work with the PHP ereg functions. You will
need to move to the preg_ fucntions in order to get non-greedy matching.
The good news is that they're not any harder, they're faster, and they have
greater functionality.
The example marty gave will work with the preg_replace function. Check the
PHP manual for the arguments. But I believe they're the same as
ereg_replace.
My tip for new PHP coders out there is to forget the ereg_ fucntions and use
the pcre functions exclusively.
-j
More information about the thelist
mailing list