RE: [PHP-DB] how does this regular expression works
| From: | Shahriyar Imanov | Date: | Wed, 13 Jul 2011 13:04:39 +0000 |
| Subject: | RE: [PHP-DB] how does this regular expression works | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-47921@lists.php.net to get a copy of this message | ||
That Regex actually has an unnecessary part to it - it would be better to
write it like:
/\<head.*?\>(?P<head_tag_innerHTML>.*?)\<\/head\>/sim
I added /m modifier, telling it that the search is performed in Multiline
basis. Added "head_tag_innerHTML" named reference for you, so writing this
Regex in PHP like this:
preg_match( '/\<head.*?\>(?P<head_tag_innerHTML>.*?)\<\/head\>/sim'
,
$subject , $matches )
will place search results inside $matches and you can access your captured
content, i.e. HEAD's value in $matches['head_tag_innerHTML'].
I also replaced your delimiters for better clearance...
It will fetch EVERYTHING that comes inside HEAD tag, i.e. its value/innerHTML.
Putting ? after * makes it to perform *ungreedy* scan. And escaping all
possible Regex characters [including <, >, ? etc] is a good practice.
Shehi
-----Original Message-----
From: who.cat@gmail.com [mailto:who.cat@gmail.com] On Behalf Of who.cat
Sent: 13 iyul 2011 15:43
To: php-db@lists.php.net
Subject: [PHP-DB] how does this regular expression works
Reading some code from the web spider,got the such a expression :
preg_match("@<head[^>]*>(.*?)<\/head>@si",$file, $regs); It's about
get the
head content of a website,i wanna know the match detail .Could someone give
some tips , thanks in advance .
All you best
------------------------
What we are struggling for ?
The life or the life ?
Attachment: [application/pkcs7-signature] smime.p7s
Attachment: [application/pkcs7-signature] smime.p7s