Reputation: 3277
CKEditor is adding new lines in my content that's entered into my database, which is fine, except that this data needs to be rendered to javascript as a single line of html.
In my PHP I have:
$tmpmaptext = $map['maptext'][$this->config->get('config_language_id')];
$tmpmaptext = html_entity_decode($tmpmaptext, ENT_QUOTES, 'UTF-8');
$tmpmaptext = str_replace(array('\r\n', '\n', '\r', '\t'), '', $tmpmaptext);
$tmpmaptext = str_replace(PHP_EOL, '', $tmpmaptext);
Which is pretty much everything I can find on how to remove new lines but I'm still ending up with this in the page:
var infowindow01 = new google.maps.InfoWindow({
content: '<div><h2>Title</h2>
<p>Address line 1,<br />
Address line 2</p>
<p>phone number</p>
<p><a href="http://www.example.com" target="_blank">www.example.com</a></p>
</div>'
How can I get all these new lines out without removing that normal spacing between characters?
Upvotes: 1
Views: 3448
Reputation: 3277
This did the trick:
$tmpmaptext = $map['maptext'][$this->config->get('config_language_id')];
$tmpmaptext = preg_replace('/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', '', $tmpmaptext);
Was able to remove all the rest and it renders perfectly now.
Upvotes: 0
Reputation: 660
I think it's because you're using single-quotes in str_replace which will search for the string '\n' (slash n). If you use double-quotes it will convert \n to the newline character...
$maptext = '<div><h2>Title</h2>
<p>Address line 1,<br />
Address line 2</p>
<p>phone number</p>
<p><a href="http://www.example.com" target="_blank">www.example.com</a></p>
</div>';
$no_newlines = str_replace(array("\n", "\r\n", "\r", "\t", " "), "", $maptext);
echo($no_newlines);
outputs:
<div><h2>Title</h2><p>Address line 1,<br />Address line 2</p><p>phone number</p><p><a href="http://www.example.com" target="_blank">www.example.com</a></p></div>
Upvotes: 2