2

I have HTML like

<td class="td_scheda_modello_dati">

       <img src="/webapp/safilo/gen_img/p_verde.gif" width="15" height="15" alt="" border="0">

</td>

I want to extract the img src from this HTML using preg_match_all().

I have done this

preg_match_all('#<td class=td_scheda_modello_dati>(.*)<td>#',$detail,$detailsav);

It should give the whole img tag.But it doesn't give me the img tag. So what changes should be done to get the specific value?

0

4 Answers 4

5

Long story short: ideone

You should not use Regex, but instead an HTML parser. Here's how.

<?php
$html = '<img src="/webapp/safilo/gen_img/p_verde.gif" width="15" height="15" alt="" border="0">';
$xpath = new DOMXPath(@DOMDocument::loadHTML($html));
$src = $xpath->evaluate("string(//img/@src)");
echo $src;
?>
Sign up to request clarification or add additional context in comments.

2 Comments

While an HTML parser is the ideal solution, I have a feeling he is looking for a much more short term solution. Also the parser might invalidly correct required quirks on output.
I think he might not be aware that an HTML parser can be used to achieve such tasks instead of Regex. I have given him a decent solution for that matter.
1

Try this code.

$html_text =  '<td class="td_scheda_modello_dati">   
            <img src="/webapp/safilo/gen_img/p_verde.gif" width="15" height="15" alt=""    border="0"></td>';

preg_match( '/src="([^"]*)"/i', $html_text , $res_array ) ;

print_r($res_array);

2 Comments

Kumar, I have multiple img tags in html, what if i just want the src of tags that are in <td class="td_scheda_modello_dati"> ... </td> ???
MJQ, I'm all for quick regex fixes but if there are multiple images in each table cell it will require 2 regexes to get all the src and be more error prone.
1

Try using the s modifier after your regex. The default behavior for the dot character is not to match newlines (which your example has).

Something like:

preg_match_all('#<td class=td_scheda_modello_dati>(.*)</td>#s',$detail,$detailsav);

Should do the trick.

It's worth reading up a bit on modifiers, the more you do with regex the more useful they become.

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Edit: also, just realized that the code posted was missing a closing td tag (it was <td> instead of </td>). Fixed my example to reflect that.

Comments

0

Try this: <img[^>]*src="([^"]*/gen_img/p_verde.gif)"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.