Jump to content

get <img tags


zapacila89

Recommended Posts

Hi i have html source from witch i want to extract all <img src="..." title="narcis" >  tags  (see the title="narcis") each img tag must have that atribute with that waxct value.

 

 

till now i comed up with this:

 

/<img src=(\"|\')(.*?)(\'|\")(.*?)title(.*?)>/s

 

lets say that i have

:

 

<img src='http://www.tizag.com/pics/tizagSugar.jpg' alt='Tizag Tutorials' title='narcis' />

 

 

it works fine.. but if i try to also sarch for the NARCIS value it wont return nothing..

 

i used:

 

/<img src=(\"|\')(.*?)(\'|\")(.*?)title='narcis'(.*?)>/s

 

what am i doing wrong??

 

 

Link to comment
https://forums.phpfreaks.com/topic/50389-get/
Share on other sites

It works for me. You need to escape the single quotes if they are used to encapsulate the pattern. Also, use a character class for the quotes, not an alternation. Keep in mind that this pattern will only work if the attributes are always in that order; otherwise, a callback would be better.

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-248172
Share on other sites

This is the function that you need

------------------------------------------

<?php
function track_img($str)
  {
    preg_match_all('/<img[\s]+(.*)>/', $str, $m, PREG_SET_ORDER);
    $i = 0;
    foreach($m as $val):
      $core = trim($val[1]);
      unset($m);
      $core = preg_split('/["|\']\s/', $core);
      foreach($core as $val)
        {
          preg_match('/(.*)\s*=\s*["|\']?(.*)["|\']?/', $val, $m);
          $ret[$i][$m[1]] = trim(trim($m[2], '"'), "'");
        }
      $i++;
      endforeach;
    unset($core);
    unset($i);
    unset($m);
    return $ret;
  }
$str = '<img src="loc" name = "my_name" title = "my_title">
<img src="loc2" name = "my_name2" title = "my_title2">';
$get = track_img($str);
print_r($get);
?>

Output

-------------------------------------------

Array
(
    [0] => Array
        (
            [src] => loc
            [name ] => my_name
            [title ] => my_title
        )

    [1] => Array
        (
            [src] => loc2
            [name ] => my_name2
            [title ] => my_title2
        )

)

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-248175
Share on other sites

OOPS the previous one had a Bug Use this one

<?php
function track_img($str)
  {
    preg_match_all('/<img[\s]+(.*)>/', $str, $m, PREG_SET_ORDER);
    $i = 0;
    foreach($m as $val):
      $core = trim($val[1]);
      unset($m);
      $core = preg_split('/["|\']\s/', $core);
      foreach($core as $val)
        {
          preg_match('/(.*)\s*=\s*["|\']?(.*)["|\']?/', $val, $m);
          $ret[$i][trim(trim(trim($m[1]), '"'), "'")] = trim(trim($m[2], '"'), "'");
        }
      $i++;
      endforeach;
    unset($core);
    unset($i);
    unset($m);
    return $ret;
  }
$str = '<img src="loc" name = "my_name" title = "my_title">
<img src="loc2" name = "my_name2" title = "my_title2">';
$get = track_img($str);
print_r($get);
?>

Output

-----------------------------------------

Array
(
    [0] => Array
        (
            [src] => loc
            [name] => my_name
            [title] => my_title
        )

    [1] => Array
        (
            [src] => loc2
            [name] => my_name2
            [title] => my_title2
        )

)

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-248182
Share on other sites

...or not, actually:

 

<pre>
<?php
$content = <<<CONTENT
<img src='http://www.tizag.com/pics/tizagSugar.jpg' alt='Tizag Tutorials' title='narcis' />
<img src='http://www.tizag.com/pics/tizagSugar.jpg' alt='Tizag Tutorials' title='narcissist' />
<img title='narcis' src='http://www.google.com/intl/en_ALL/images/logo.gif' alt='Google' />
CONTENT;
preg_match_all('/<img[^>]+?title=[\'"]narcis[\'"][^>]*?>/', $content, $matches);
print_r($matches);
?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-249166
Share on other sites

i ant to replace or remove all that other images from html string if they dont have a id="narcis"

 

Don't you mean title? If not, just change the pattern:

 

<pre>
<?php
$content = <<<CONTENT
<img src='http://www.tizag.com/pics/tizagSugar.jpg' alt='Tizag Tutorials' title='narcis' />
<img src='http://www.tizag.com/pics/tizagSugar.jpg' alt='Tizag Tutorials' title='narcissist' />
<img title='narcis' src='http://www.google.com/intl/en_ALL/images/logo.gif' alt='Google' />
<img title='' src='http://www.google.com/intl/en_ALL/images/logo.gif' alt='Google' />
<img src='http://www.google.com/intl/en_ALL/images/logo.gif' alt='Google' />
CONTENT;
echo preg_replace('/<img(??!title=[\'"]narcis[\'"])[^>])+>/', '', $content);
?>
</pre>

 

Sorry, maybe a bit of topic, but I never seen

<<<CONTENT CONTENT;

 

Is it same like <<<ECHO  END; but passes the value to a variable?

 

I checked the PHP manual and could not find it. Does it work in PHP 4?

 

Thanx

 

It's called heredoc.

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-249764
Share on other sites

no i need now id i chnaged my mind, any thats not a problem..

 

function getFirtNarcisImage($htmll)

{

$matches;

    preg_match_all('/<img[^>]+?id=[\'"]narcis[^>]*?>/', $html, $matches);

echo $matches[0];

}

i also changed from narcis[\'"]  to narcis[^>]... i didnt wanted just narcis there.. 

 

and used as $html :

 

<img id="narcis|~| tralalala |~|80;80" src="http://localhost/images/stories/clock.jpg" alt=" " width="150" height="112" />

 

output:

Array

 

also tried $matches[0][0] still nothing.. it shows at count()  that it has a member.. but i think its nothinf in it..

 

any ideeas what i did wrong??

 

 

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-249782
Share on other sites

wow.... really sorry.. about that  ;D I guess it happens.. real tierd..

 

anyway it still doesnt work

 

print r just outputs :

 

Array => Array();

 

i use this text...

 

 

<p>b<img id="narcis|~| |~|80;80" src="http://localhost/images/stories/clock.jpg" alt=" " width="150" height="112" />bb bbbbbbbbbbbbbb<img src="http://localhost/images/stories/key.jpg" alt=" " width="150" height="112" />bbbbbfsd</p><p>fsdfdsf</p><p>fdsf</p><p>ds</p><p>fdsffffffffffff f fdfdsfdsfd</p><p>fdsfdsfdaaaaaaaaaaa

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-249903
Share on other sites

<pre>
<?php
$content = <<<CONTENT
<p>b<img id="narcis|~| |~|80;80" src="http://localhost/images/stories/clock.jpg" alt=" " width="150" height="112" />bb bbbbbbbbbbbbbb " width="150" height="112bbbbbfsd</p><p>fsdfdsf</p><p>fdsf</p><p>ds</p><p>fdsffffffffffff f fdfdsfdsfd</p><p>fdsfdsfdaaaaaaaaaaa
CONTENT;
function getFirtNarcisImage($html) {
	preg_match_all('/<img[^>]+?id=[\'"]narcis[^>]*?>/', $html, $matches);
	print_r($matches);
}
getFirtNarcisImage($content);
?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/50389-get/#findComment-249916
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.