Solved

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

Posted on 2011-02-18
7
290 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now