Jump to content

[SOLVED] Stripping html text and newline characters from string


jwhite68

Recommended Posts

I have used the strip_tags function to remove html from a string with reasonable success. But it still leaves /r/n characters. I've hunted around on various forums for solutions, and every time someone suggests a solution, it actually doesnt work.

 

Here is the code:

$html_string='<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US">The village of Sechishte is 40 km from the town of Shumen and 90 km from Varna city.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>\r\n<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US"> <o:p></o:p></SPAN></P>\r\n<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US">This 120 sq m house, consisting of 6 rooms has a huge piece of land of 5 000 sq m. It is a corner plot with an additional small house and a barn on it.<o:p></o:p></SPAN></P>\r\n<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US">Bathroom/WC are on the ground floor of the house.<o:p></o:p></SPAN></P>';
$string=strip_tags($html_string);
echo $string;

$value = eregi_replace("\n\r","",$string);
echo $value;

 

The forst echo show the strip of html which leaves in the \r\n newlines.  The second is one solution a forum suggested for removing newlines which doesnt work.

Anyone got any suggestions?

 

Link to comment
Share on other sites

Try:

<?php

$html_string = '<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US">The village of Sechishte is 40 km from the town of Shumen and 90 km from Varna city.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>\r\n<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US"> <o:p></o:p></SPAN></P>\r\n<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US">This 120 sq m house, consisting of 6 rooms has a huge piece of land of 5 000 sq m. It is a corner plot with an additional small house and a barn on it.<o:p></o:p></SPAN></P>\r\n<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-ansi-language: EN-US">Bathroom/WC are on the ground floor of the house.<o:p></o:p></SPAN></P>';
$string = strip_tags($html_string);

echo $string;

echo '<hr />';

$value = str_replace('\r\n', '', $string);

echo $value;

?>

 

 

 

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.