Jump to content

[SOLVED] Cutting a string


ballouta

Recommended Posts

Hello

 

I have press releases in my DB, I want to take ONLY the first paragraph of each article and place a read more button to read the full news.

 

Each first paragrapgh ends with either </p> or </br> (because a Rick text editor is being used to insert the text)

 

So how i can take only the first paragrapgh of each news please?

 

Many thanks in advance.

Link to comment
Share on other sites

If every paragraph ends with '</p>' you can use something like this:

 

$row = mysql_fetch_assoc($result); //From the query you made..
$output = str_replace('</p>', '</p>{END_PARAGRAPH}', $row['entry']); // Or whatever row you have the article in..
$output = explode('{END_PARAGRAPH}', $output); 
echo $output[0];

Link to comment
Share on other sites

Could also do it this way as ... but there are probably better ways to do it.

 

<?php

# This would be your press release populated form your database
$press_release = "<p>This is my press release. It is pretty generic.</p>
				<p>It really doesn't tell you anything interesting</p>";

# This would ideally be generated from your database
$link = 'http://news.com/news?id=1';					

# Explode into an array splitting the press release at </p> doing it this way will split the string BEFORE the </p>
$first_para = explode('</p>', $press_release);

# Remember we need to add the traling </p> tag as we've cut it off

echo $first_para[0] . '<a href="' . $link . '">...Read More</a></p>';

Link to comment
Share on other sites

Could also do it this way as ... but there are probably better ways to do it.

 

<?php

# This would be your press release populated form your database
$press_release = "<p>This is my press release. It is pretty generic.</p>
				<p>It really doesn't tell you anything interesting</p>";

# This would ideally be generated from your database
$link = 'http://news.com/news?id=1';					

# Explode into an array splitting the press release at </p> doing it this way will split the string BEFORE the </p>
$first_para = explode('</p>', $press_release);

# Remember we need to add the traling </p> tag as we've cut it off

echo $first_para[0] . '<a href="' . $link . '">...Read More</a></p>';

The reason I didn't do it like that is because then you manually have to append the closing paragraph tag. I guess it's not to bad since you also have to append the link.

Link to comment
Share on other sites

Maybe something different.

This removes the <p> tags

throws in some css styles

for the story and for the read more link

<head>
<style type="text/css">
.story {color:sienna}
.readmore {color:blue}
</style>
</head>
<?php

    $prel = "<p>This is my press release. It is pretty generic.</p>
<p>It really doesn't tell you anything interesting</p>";

if(!preg_match('/\<p\>(.*)?\<\/p\>/i',$prel,$pr))
    $pr=null;
else
    $pr="<span class=\"story\">{$pr[1]}</span> <span class=\"readmore\"><a href=\"story.php?id=1\">... Read More ...</a></span>";

echo $pr;    
?>

 

Yes, I could have left the <p> tags in place, but that drops the read more link.

I guess ya can put them back in:)

 

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.