Re: preg guru question
| From: | joel boonstra | Date: | Fri, 13 Feb 2004 19:19:11 +0000 |
| Subject: | Re: preg guru question | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-177630@lists.php.net to get a copy of this message | ||
On Sat, Feb 14, 2004 at 03:05:40AM +0800, Jason Wong wrote:
> $doo[1] = '<img src="dah.jpg">';
> $doo[2] = "<img src='doo.jpg'>";
>
> foreach ($doo as $dah) {
> if (preg_match('/<img src=["|\'](.*)\.(.*)["|\']>/i',
> $dah, $matches)) {
> print_r($matches);
> }
> }
Note that characters within square brackets don't need to have the | to
separate them; it's a character class, so it will match any character
within the brackets:
if (preg_match('/<img src=["\'](.*)\.(.*)["\']>/i', $dah,
$matches)) {
Also, it's probably good to put a non-greedy modifier after the *s to
prevent .* from matching too much. Compare:
<?
$doo[1] = '<img src="dah.jpg"><img src="blah.jpg">';
$doo[2] = "<img src='doo.jpg'>";
foreach ($doo as $dah) {
if (preg_match('/<img src=["\'](.*)\.(.*)["\']>/i', $dah,
$matches)) {
print_r($matches);
}
# adding ? after the *
if (preg_match('/<img src=["\'](.*?)\.(.*?)["\']>/ig', $dah,
$matches)) {
print_r($matches);
}
}
?>
'Course, it still doesn't match multiple images in one line.
preg_match_all() could probably help out there.
joel
--
[ joel boonstra | gospelcom.net ]