Jump to content

Extract first paragraph with span, no image ARGH!


richrock

Recommended Posts

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!?!!

<?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.

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.