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 ]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to