Solved

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

Posted on 2011-02-18
7
292 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 49
Display Calender only  When events is scheduled in Worpdress? 1 23
Configuring a checkbox in CSS and php 18 88
does post require a form or curl to be post 4 30
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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