matthewtbaker Posted October 4, 2012 Share Posted October 4, 2012 Hi, I have a string which I am pulling from a MySQL DB which unfortunately looks like the following. <p><img src="myimage.jpg" border="0" /></p> <p>My short paragraph</p> I would like to pull out <img src="myimage.jpg" border="0" /> and place it in a separate variable 'strImage' (so I can echo it elsewhere), and then place the remainder of the string in a second variable 'strText'. Note: The <img src> and <border> values will vary depending on the article I pull from the database. What method should I use to make this happen? Thank you so much for your help in advance! Matt Quote Link to comment https://forums.phpfreaks.com/topic/269066-split-string-at-tag-and-place-in-new-varaible/ Share on other sites More sharing options...
Zane Posted October 4, 2012 Share Posted October 4, 2012 (edited) PHP´s strip_tags function should do the trick http://php.net/strip-tags $html = '<p><img src="myimage.jpg" border="0" /></p> <p>My short paragraph</p>'; $strImage = strip_tags($html, "<img>"); I suppose you could do the same for your second varialbe, but you will have to come up with something to get rid of blank P tags. Edited October 4, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/269066-split-string-at-tag-and-place-in-new-varaible/#findComment-1382618 Share on other sites More sharing options...
matthewtbaker Posted October 4, 2012 Author Share Posted October 4, 2012 (edited) Hi Zane, Thanks for solution. Unfortunately it does not do exactly what I need. It does remove the image perfectly when allowing the tag <p>, but when using your example of allowing <img> it does not. Probably because the the <p> tags are closed so the text is still returned? $html = '<p><img src="myimage.jpg" border="0" /></p> <p>My short paragraph</p>'; $strImage = strip_tags($html, "<p>"); Returns <p></p><p>My short paragraph</p> Which is great, but $html = '<p><img src="myimage.jpg" border="0" /></p> <p>My short paragraph</p>'; $strImage = strip_tags($html, "<img>"); returns <img src="myimage.jpg" border="0" />My short paragraph So how can I put the <img> info into another variable? Thanks again. Edited October 4, 2012 by matthewtbaker Quote Link to comment https://forums.phpfreaks.com/topic/269066-split-string-at-tag-and-place-in-new-varaible/#findComment-1382619 Share on other sites More sharing options...
Zane Posted October 4, 2012 Share Posted October 4, 2012 There is always the DOMDocument route. $doc=new DOMDocument; $doc->loadHTML('<p><img src="myimage.jpg" border="0" /></p> <p>My short paragraph</p>'); $path=new DOMXPath($doc); foreach ($path->query('//img') as $found) echo $doc->saveXML($found); Quote Link to comment https://forums.phpfreaks.com/topic/269066-split-string-at-tag-and-place-in-new-varaible/#findComment-1382620 Share on other sites More sharing options...
matthewtbaker Posted October 4, 2012 Author Share Posted October 4, 2012 Thank you Zane! These solutions work perfectly. I have used a combination of both your DOM route and the strip_tags function to get my desired result. Quote Link to comment https://forums.phpfreaks.com/topic/269066-split-string-at-tag-and-place-in-new-varaible/#findComment-1382629 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.