Jeffro Posted May 2, 2011 Share Posted May 2, 2011 (If this is a regex question and should be moved, I apologize.. wasn't sure since I don't know the answer) If my $description is the following, how would I strip away all the stuff before the list and after it and keep only the contents of the ul?: random text.. blah blah blah.. more text.. blah blah list item 1 list item 2 list item 3 random text.. blah blah blah.. more text.. blah blah Quote Link to comment https://forums.phpfreaks.com/topic/235364-how-do-keep-only-text-inside-a-list/ Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 you could use a combination of trim(), strpos(), and substr() to select the code of the list Quote Link to comment https://forums.phpfreaks.com/topic/235364-how-do-keep-only-text-inside-a-list/#findComment-1209539 Share on other sites More sharing options...
sunfighter Posted May 2, 2011 Share Posted May 2, 2011 This was just done over the weekend. It will yield results that can be displayed in a html file: <?php $garbage = 'just some bs <ul><li>text</li><li>text</li><li>text</li></ul>just some more bs'; preg_match_all('/<ul>(.*?)<\/ul>/i', $garbage, $matches); echo $matches[0][0]; ?> $matches[1][0]; will not include the <ul> tags. And with a little work you could get rid of the <li> tags also. Quote Link to comment https://forums.phpfreaks.com/topic/235364-how-do-keep-only-text-inside-a-list/#findComment-1209553 Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 you could also echo out the $matches in a foreach loop Quote Link to comment https://forums.phpfreaks.com/topic/235364-how-do-keep-only-text-inside-a-list/#findComment-1209555 Share on other sites More sharing options...
Jeffro Posted May 2, 2011 Author Share Posted May 2, 2011 Thx to all! Sunfighter's code did the trick perfectly for me! Quote Link to comment https://forums.phpfreaks.com/topic/235364-how-do-keep-only-text-inside-a-list/#findComment-1209593 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.