ballouta Posted June 3, 2009 Share Posted June 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/ Share on other sites More sharing options...
Alex Posted June 3, 2009 Share Posted June 3, 2009 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]; Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/#findComment-848848 Share on other sites More sharing options...
keeps21 Posted June 3, 2009 Share Posted June 3, 2009 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/#findComment-848853 Share on other sites More sharing options...
Alex Posted June 3, 2009 Share Posted June 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/#findComment-848856 Share on other sites More sharing options...
laffin Posted June 4, 2009 Share Posted June 4, 2009 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:) Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/#findComment-848930 Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 1. </br> isn't even a valid HTML tag. 2. What do you mean a <p> either ends with a </p> or <br /> (corrected)? So you're saying there are some paragraphs that ends with a <br /> but not a </p>? Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/#findComment-848988 Share on other sites More sharing options...
ballouta Posted June 4, 2009 Author Share Posted June 4, 2009 Hi Thanks Ken2k7, I will leave the <br /> tags. laffin & AlexWD, thanks you, I will test your code Quote Link to comment https://forums.phpfreaks.com/topic/160840-solved-cutting-a-string/#findComment-849070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.