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? Quote Link to comment 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); Quote Link to comment 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 Quote Link to comment 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. 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.