[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ereg and img tag

Posted on 2008-11-13
5
Medium Priority
?
359 Views
Last Modified: 2012-05-05
Hi experts

I need to retreive the value of the "src" from the tag "img" into an html string.

for exemple:
html -> this is a test <img src='test.jpg'> test
also html -> this is a test <img src="test.jpg"> test
It should return "test.jpg"
0
Comment
Question by:cabou
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22950151
<?php

$text = "this is a test <img src='test.jpg'> test";
$pattern = "(.*)<img src='(.+)'>(.*)";
$matches = array();

ereg( $pattern, $text, $matches );

print_r($matches);
echo "<br/>";
echo "The text you what is in the 2nd group and is '". $matches[2] . "'";
?>
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22950174
Note: The pattern used above (.*)<img src='(.+)'>(.*) expects <image and src=' to be separated by one space and for thesrc to be delimited by apostrophes rather than double quotes.
0
 

Author Comment

by:cabou
ID: 22951409
I need a solution the works with apostrophes and double quotes

thanks
0
 
LVL 11

Accepted Solution

by:
jausions earned 2000 total points
ID: 22951543
First, you shouldn't be using the ereg function, it's on its way out. Use PCRE functions instead... preg_match()

Here's a regular expression that actually works, and actually capture all the images if needed...
<?php
 
$html = array(
    'blah test <img alt="blue"
src ="image.jpg" width="12" /> test message',
    '<img src=\'test.jpg\'>',
    '<p>some text<img lowsrc="blackwhite.gif" src="fullcolor.jpg" title="Low/Res"/> <img src="icon.png" height="12"></p>',
);
 
 
 
$re = '/<img\s*[^>]*\s+src\s*=\s*([\'"])([^"\']+)\1\s*[^>]*>/im';
 
foreach ($html as $h) {
    // Capture all the <img> information
    preg_match_all($re, $h, $matches);
 
    echo "\r\n".$h.' ===> ';
    print_r($matches[2]);
    echo "\r\n";
 
    // Captures only the first match
    preg_match($re, $h, $matches);
 
    echo "\r\n".$h.' ===> '.$matches[2]."\r\n";
 
}
 
?>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question