zapacila89 Posted May 7, 2007 Share Posted May 7, 2007 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?? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 8, 2007 Share Posted May 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 8, 2007 Share Posted May 8, 2007 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 ) ) Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 8, 2007 Share Posted May 8, 2007 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 ) ) Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 9, 2007 Author Share Posted May 9, 2007 a callback you say? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 9, 2007 Share Posted May 9, 2007 ...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> Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 9, 2007 Share Posted May 9, 2007 a callback you say? Why do you need a callback cause teh above code is exploding(). and its working fine here. Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 10, 2007 Author Share Posted May 10, 2007 thanks.. and how would i do to replace all other images with a nlank space " " .. execept those ?? Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 10, 2007 Share Posted May 10, 2007 thanks.. and how would i do to replace all other images with a nlank space " " .. execept those ?? I didn't understand Quote Link to comment Share on other sites More sharing options...
igor berger Posted May 10, 2007 Share Posted May 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 10, 2007 Share Posted May 10, 2007 Its for PHP 5 Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 10, 2007 Author Share Posted May 10, 2007 well i have a html string with normal images.. and images ith that id="narcis" i ant to replace or remove all that other images from html string if they dont have a id="narcis" Quote Link to comment Share on other sites More sharing options...
effigy Posted May 10, 2007 Share Posted May 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 10, 2007 Author Share Posted May 10, 2007 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?? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 10, 2007 Share Posted May 10, 2007 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). Quote Link to comment Share on other sites More sharing options...
neel_basu Posted May 10, 2007 Share Posted May 10, 2007 What you are trting to d actually ?? Just getting all the values of all the attributes of all img Tags ?? Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 10, 2007 Author Share Posted May 10, 2007 wow.... really sorry.. about that 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 Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 10, 2007 Author Share Posted May 10, 2007 well to be short: i need a function to which i pass a string(html).. and in turn it returns me the first image that has that id="narcis.." and then i want to remove from that string all other images that don`t have that id Quote Link to comment Share on other sites More sharing options...
effigy Posted May 10, 2007 Share Posted May 10, 2007 <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> Quote Link to comment Share on other sites More sharing options...
zapacila89 Posted May 10, 2007 Author Share Posted May 10, 2007 thanks man. i really apreciate it. and sorry for my english.. gets kinda messy when i`m tired. Cheers! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.