richrock Posted June 15, 2010 Share Posted June 15, 2010 Hi, I'm trying to extract the first paragraph from a text entry in the database, to show a list of items. I've set up some sample data like this: <p class="team_intro"><img src="john_smith.jpg" /><span class="leadername">John Smith</span> - a great funny witty super guy. </p><p>Rest of the text blabh blablahvlvlvlvdfjerfh etc... </p> That is the data I would retrieve in my for loop, and I can't do much about image position, as the editor (joomla) seems to place it inside the first paragraph. I can't really use ID's in the p or img tags as joomla doesn't handle that. I have managed to extract the image using some code from another source: $intro = $teamMembers[$i]->introtext; $intro_clean = preg_replace("/<div[^>]+\>/i", "", $intro); // Strips any divs in case they are inserted from C&P text $intro_image = preg_match("/<img[^>]+\>/i", $intro_clean, $matches); $intro_clean = preg_replace("/<img[^>]+\>/i", "", $intro); I've tried to extract the first paragraph using: preg_match("/<p>(.*)<\/p>/",$intro_clean,$names); $introsentence = strip_tags($names[0]); //removes anchors and other tags from the intro but it doesn't work. How to I get the whole first paragraph? Help!?!! Link to comment https://forums.phpfreaks.com/topic/204850-extract-first-paragraph-with-span-no-image-argh/ Share on other sites More sharing options...
Psycho Posted June 15, 2010 Share Posted June 15, 2010 <?php $input = '<p class="team_intro"><img src="john_smith.jpg" /><span class="leadername">John Smith</span> - a great funny witty super guy. </p><p>Rest of the text blabh blablahvlvlvlvdfjerfh etc... </p>'; //Get the first paragraph and put into $match array var preg_match('#<p[^>]*>(.*?)</p>#', $input, $match); //Remove images from the result $first_para = preg_replace('#<img[^>]*>#', '', $match[1]); echo $first_para; //Output: <span class="leadername">John Smith</span> - a great funny witty super guy. ?> Link to comment https://forums.phpfreaks.com/topic/204850-extract-first-paragraph-with-span-no-image-argh/#findComment-1072653 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.