Solved

I need to convert multiple paragraphs to a single line in php

Posted on 2011-02-18
7
295 Views
Last Modified: 2012-05-11
In doing so, I need to strip out all <p> tags and convert all </p> tags to \n I am using a CMS with a calendar, but the ical export, while replacing the </p> with \n isn't putting it all on one line.

here is the code i have originally:
$description = strip_tags(str_ireplace('<br />', '\n', $this->getDescription()));

Open in new window


which was for <br /> I need it to do this for <br /> and </p> AND put it all on one line, no carriage returns.
0
Comment
Question by:acprog2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 34927878
$description = str_ireplace('<br />', '\n', $this->getDescription());
$description = strip_tags(str_ireplace('</p>', '\n', $description));
0
 

Author Comment

by:acprog2
ID: 34927892
half way. now i need the output on a single line. with the \n in place of the br and p tags.
0
 

Author Comment

by:acprog2
ID: 34928042
ok I have come up with this:
$description = trim( preg_replace( '/\s+/', ' ', $this->getDescription() ) );

$description = strip_tags(str_ireplace(array('\r','</p>','<br />'), '\n', $this->getDescription()));

both line work, but not together. the first puts everything on one line, the second replaces my tags properly. but my script is only respecting the second line if both are there, and the first if the second is commented out.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 14

Accepted Solution

by:
svgmuc earned 250 total points
ID: 34928253
You're saving the result in a variable, and then get it again from the object instance.

$description = trim( preg_replace( '/\s+/', ' ', $this->getDescription() ) );

$description = strip_tags(str_ireplace(array('\r','</p>','<br />'), '\n', $description()));
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 250 total points
ID: 34928255
How about
$description = trim( preg_replace( '/\s+/', ' ', $this->getDescription() ) );
$description = strip_tags(str_ireplace(array('\r','</p>','<br />'), '\n', $description));
0
 
LVL 14

Expert Comment

by:svgmuc
ID: 34928700
yeah, sorry, forgot to remove the parentheses
0
 

Author Closing Comment

by:acprog2
ID: 34928894
Thanks, both of you.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question