fraser5002 Posted April 7, 2011 Share Posted April 7, 2011 Hi i have a string which contains some text and 2 links for example. How could i extract all the string minus one of the links eg $textstring = "some text before <a href="testpage.php"> <img src="image1.jpg" /></a> <a href="testpage2.php"> <img src="image2.jpg" /></a> some text after " i would like to make a create a new string which contains all this text minus the first link $newstring = "some text before <a href="testpage2.php"> <img src="image2.jpg" /></a> some text after " any ideas? Link to comment https://forums.phpfreaks.com/topic/233011-extract-from-string/ Share on other sites More sharing options...
Psycho Posted April 7, 2011 Share Posted April 7, 2011 If it is ALWAYS the first link then you could do this $newstring = preg_replace("#<a[^>]*>.*?<\/a>#", '', $textstring, 1); Link to comment https://forums.phpfreaks.com/topic/233011-extract-from-string/#findComment-1198407 Share on other sites More sharing options...
fraser5002 Posted April 7, 2011 Author Share Posted April 7, 2011 hi thanks the the reply it nearly works for me but i run into a problem when the <a is not separated by a space from the characters before it i.e sometext<a href="blabla.php"> link1</a> it does not seem to be able to pick up that first <a> tag and im assuming its because there is no space between the text and the start of the <a is this right? Thanks Fraser Link to comment https://forums.phpfreaks.com/topic/233011-extract-from-string/#findComment-1198432 Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 I don't think that's the problem. Maybe you should post the real string that fails. Link to comment https://forums.phpfreaks.com/topic/233011-extract-from-string/#findComment-1198452 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.