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?

MJQ
  • 1,778
  • 6
  • 34
  • 60
  • possible duplicate of [How to extract img src, title and alt from html using php?](http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php) – Toto Jan 03 '14 at 11:39
  • Could you please close the question if my answered helped? – Ali Gajani Jan 15 '14 at 03:09
  • @AliGajani I got help from multiple answers! So, can't mark a single answer as accepted! Thanks for your help :) – MJQ Jan 15 '14 at 06:20
  • @MJQ: I agree, but still mark "some answer" as a green! – Ali Gajani Jun 10 '14 at 03:11

4 Answers4

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;
?>
Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • 2
    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. – Flosculus Jan 03 '14 at 11:43
  • 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. – Ali Gajani Jan 03 '14 at 11:43
  • 1
    Well +1 for best practice. – Flosculus Jan 03 '14 at 11:45
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);
Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • Kumar, I have multiple img tags in html, what if i just want the src of tags that are in ... ??? – MJQ Jan 03 '14 at 11:49
  • 1
    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. – OGHaza Jan 03 '14 at 12:04
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.

leo
  • 339
  • 2
  • 13
0

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

user2936213
  • 1,021
  • 1
  • 8
  • 19