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

Link to comment
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.