NYCPHP Meetup

NYPHP.org

[nycphp-talk] strange regex problem

David Sklar sklar at sklar.com
Mon Aug 11 19:03:44 EDT 2003


A few things make this nasty:

^.*<title>([^<]*)</title>.*$

With the anchors and the leading and trailing .*'s, you are matching against
the entire document. Plus, since * is greedy by default, the initial .* (I
think) has to match the entire document, then backtrack to the last <title>
tag in the doc. Then, the trailing .* has to do some matching after the
</title> tag is found, matching that doesn't affect what gets captured at
all.

David


On Monday, August 11, 2003 6:57 PM,  wrote:

> Thanks!
>
> That works nicely.
>
> Any thoughts on what would make previous regex so nasty?
>
> FB`
>
> ----- Original Message -----
> From: "David Sklar" <sklar at sklar.com>
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Sent: Monday, August 11, 2003 6:34 PM
> Subject: RE: [nycphp-talk] strange regex problem
>
>
>> If you just want the title out of the document, try this:
>>
>> if (preg_match('@<title>(.*?)</title>@i',$str, $matches)) {
>> $title = $matches[1]; } else {
>>     print "There's no title";
>> }
>>
>> David
>>
>>
>> On Monday, August 11, 2003 5:58 PM,  wrote:
>>
>>> I have an apparently valid regular expression,
>>> ^.*<title>([^<]*)</title>.*$
>>> that works well in one site, but causes the server hosting a
>>> different site to grind its teeth to nubs on it - consistently
>>> exceeding max ex time...
>>>
>>> the full line goes like this:
>>>     $text_a_title =
>>> eregi_replace("^.*<title>([^<]+)</title>.*$","\\1",$str);
>>>
>>> if i bypass this line, everything else works swimmingly...
>>>
>>>
>>> any suggestions?
>>> (taking a title from an html file)
>>>
>>>
>>> TIA
>>>
>>> FB`
>>>
>>> _______________________________________________
>>> talk mailing list
>>> talk at lists.nyphp.org
>>> http://lists.nyphp.org/mailman/listinfo/talk
>> _______________________________________________
>> talk mailing list
>> talk at lists.nyphp.org
>> http://lists.nyphp.org/mailman/listinfo/talk
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list