Perad Posted October 8, 2008 Share Posted October 8, 2008 hi, I have a long string consisting of paragraphs and other elements. I want to cut the string after the first </p>. This will stop my page from breaking. String is $content. Does anyone have any idea how to quickly and efficiently do this? Link to comment https://forums.phpfreaks.com/topic/127537-cut-string-after/ Share on other sites More sharing options...
waynew Posted October 8, 2008 Share Posted October 8, 2008 Stop your page from breaking? ??? Link to comment https://forums.phpfreaks.com/topic/127537-cut-string-after/#findComment-659829 Share on other sites More sharing options...
Andy-H Posted October 8, 2008 Share Posted October 8, 2008 $data = explode("</p>", $content); echo $data[0] . '</p>'; ?? Link to comment https://forums.phpfreaks.com/topic/127537-cut-string-after/#findComment-659831 Share on other sites More sharing options...
Perad Posted October 8, 2008 Author Share Posted October 8, 2008 Stop your page from breaking? ??? Yes, if I randomly cut a string and it is half way through a link, a span tag, an image tag, a paragraph etc there will be knock on problems down the page. Thanks Andy, having a rotten day, explode didn't occur to me. I was thinking about finding a string pattern with an expression then using substr.. i don't know what I was thinking. Link to comment https://forums.phpfreaks.com/topic/127537-cut-string-after/#findComment-659833 Share on other sites More sharing options...
Andy-H Posted October 8, 2008 Share Posted October 8, 2008 Lol I know what you mean, I always overthink things. Link to comment https://forums.phpfreaks.com/topic/127537-cut-string-after/#findComment-659840 Share on other sites More sharing options...
Orio Posted October 8, 2008 Share Posted October 8, 2008 A bit more efficient (it'll run faster and memory wise) would be using: <?php $data = substr($content, 0, strpos($content, "</p>"))."</p>"; ?> (At least according to test I've done) Orio. Link to comment https://forums.phpfreaks.com/topic/127537-cut-string-after/#findComment-659846 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.