SCordell Posted June 14, 2008 Share Posted June 14, 2008 Hello, I am a designer that is new to PHP. My question is: how can I extract part of a string, to print it? For example, I have this string: <td align="center" valign="center" style="padding:0" class="flickr_badge_image" id="flickr_badge_image1"><a href="http://www.flickr.com/photos/user/1553388976/in/set-62157605447604283/"><img src="http://farm4.static.flickr.com/3165/1553388976_68b023e8f4_m.jpg" alt="A photo on Flickr" title="Another Win" height="180" width="240"></a></td> And from the above string, I only want to extract this portion: <a href="http://www.flickr.com/photos/user/1553388976/in/set-62157605447604283/"><img src="http://farm4.static.flickr.com/3165/1553388976_68b023e8f4_m.jpg" alt="A photo on Flickr" title="Another Win" height="180" width="240"></a> What code can I use to tell PHP to get everything in between "<a href" and "a>" and only print that? I've tried "substr" and "preg_match" and "preg_match_all"... but it's clear that I do not know what I am doing. Thank you in advance for any help, SC Link to comment https://forums.phpfreaks.com/topic/110233-how-can-i-extract-part-of-a-string-to-print-it/ Share on other sites More sharing options...
Barand Posted June 14, 2008 Share Posted June 14, 2008 try $p1 = strpos($str, '<a'); $p2 = strpos($str, '</a>'); $wanted = substr($str, $p1, $p2-$p1+4); echo $wanted; Link to comment https://forums.phpfreaks.com/topic/110233-how-can-i-extract-part-of-a-string-to-print-it/#findComment-565635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.