Forum Moderators: coopster

Message Too Old, No Replies

Finding src value with regex

         

asantos

5:56 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



Hi. I need to grab the src value of the first available image tag (<img>) in this html code block:

<p>Hello this is a test. <img src="/something/picture.jpg" alt="inauguracion" title="test" class="right" />Some data here... some more other here. Here another img <img src="other.jpg"> <strong>finish</strong>.</p>

I know this must be done with regex but i am totally lost in that.

phranque

10:18 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



something like this may help and $3 would contain the source value:
<\s*(img¦IMG)[^>]*(src¦SRC)\s*=\s*['"]\s*([^'"]*\/)*[^'"\s]+\s*['"][^>]*>

(replacing broken pipes above with vertical bar symbols)

asantos

11:51 pm on Jan 17, 2007 (gmt 0)

10+ Year Member



phranke:

im using this code:

<?
$subject = '<p>Hello this is a test. <img src="/something/picture.jpg" alt="inauguracion" title="test" class="right" />Some data here... some more other here. Here another img <img src="other.jpg"> <strong>finish</strong>.</p>';
$pattern = '<\s*(img)[^>]*(src)\s*=\s*[quote]\s*([^"]*\/)*[^"\s]+\s*[quote][^>]*>';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

Its throwing me this error:

Warning: preg_match(): Unknown modifier ']' in D:\Websites\clientes\online\nikkeimotors.com\test.php on line 4

phranque

12:55 am on Jan 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



try this:


<?
$subject = '<p>Hello this is a test. <img src="/something/picture.jpg" alt="inauguracion" title="test" class="right" />Some data here... some more other here. Here another img <img src="other.jpg"> <strong>finish</strong>.</p>';
$pattern = '<\s*img[^>]*src\s*=\s*"\s*([^"]*\/)*[^"\s]+\s*"[^>]*>';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>

asantos

4:27 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Still...:

Warning: preg_match(): Unknown modifier ']' in D:\Websites\clientes\online\nikkeimotors.com\test.php on line 4

Perhaps it has something to do with your note: "
(replacing broken pipes above with vertical bar symbols) " ... i dont get that ...

asantos

5:20 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



I added a / as patterns first and last char nd now i dont get any error msgs:

$pattern = '/< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )/';

but the $matches var gets nothing at the end

asantos

7:39 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



I arranged some of the regex chars and it ended up like this:

$pattern = '/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i';

It works ok but $matches is trhrowing me this data:

Array
(
[0] => <img src="/something/picture.jpg
[1] => /something/picture.jpg
)

I only would like it to throw:
[0] => /something/picture.jpg

What changes should i implement?

phranque

10:51 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Warning: preg_match(): Unknown modifier ']' in D:\Websites\clientes\online\nikkeimotors.com\test.php on line 4

Perhaps it has something to do with your note: "
(replacing broken pipes above with vertical bar symbols) " ... i dont get that ...

the verticle bar symbol (aka "pipe" symbol, which is a unix term; aka "or" symbol in some languages such as perl) is the "upper case" for the backwards slash character on my keyboard.
any post made on this site translates the verticle bar to a broken "pipe" symbol.
the most recent regexp example does not contain any pipes.

phranque

10:56 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I added a / as patterns first and last char nd now i dont get any error msgs:

$pattern = '/< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )/';

but the $matches var gets nothing at the end

too many spaces added!
they are meaningful in a regular expression.

how you use a regular expression within a script depends on the scripting language and the context in which it is used.
you asked for the regular expression and the regular expression is contained within the slashes in your case.

asantos

11:02 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



phranque:
thanks for the replies, i already acommodated the code for php and it works!

the problem is that the preg_match function is throwing two values (instead of just one). I am using this pattern:

$pattern = '/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i';

And this is the result:

Array
(
[0] => <img src="/something/picture.jpg
[1] => /something/picture.jpg
)

I just want the result to be:
Array
(
[0] => /something/picture.jpg
)

phranque

11:04 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I arranged some of the regex chars and it ended up like this:

$pattern = '/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i';

It works ok but $matches is trhrowing me this data:

Array
(
[0] => <img src="/something/picture.jpg
[1] => /something/picture.jpg
)

I only would like it to throw:
[0] => /something/picture.jpg

What changes should i implement?

put your code in a loop and index into the string for each iteration.
then use Array[1] for each iteration since that is what you want, right?

or use preg_match_all as i suggested in my 2nd response and that will get all matches in one shot.
then use the correct array slice depending on how you call it...

i've never programmed in php, so you might want to refer to this:
[us3.php.net...]