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
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
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
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
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
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
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
Share on other sites

1. You need to watch your spelling in your code and your posts. It's confusing for readers and error prone for PHP--your function is looking for $htmll, but using $html.

 

2. You're not understanding preg_match_all's return. See the manual. For a hint, use print_r($matches).

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.