Kyle
Kyle

Reputation: 3

how to remove   character in php? (str_replace function not working)

"contentDetails" has following data in it:

<p>This is data sample.&nbsp;</p><p>Second part of the paragraph.&nbsp;</p>

str_replace is not working here. Please take a look.

here is how my xml strucuture in php looks like:

$xml = <?xml version="1.0" encoding="UTF-8">;
$xml = '<root>';
$xml = '<myData>';
$xml .= <content> . str_replace("&nbsp;", "", htmlentities($_POST[contentDetails])) . </content>
$xml = '</myData>';
$xml = '</root>';

Upvotes: 0

Views: 850

Answers (2)

fbax
fbax

Reputation: 47

The htmlentities() function converts &nbsp; to &amp;nbsp; --- so try this...

str_replace("&amp;nbsp;", "", htmlentities($_POST[contentDetails]))

Upvotes: 1

Nick
Nick

Reputation: 147146

I'm assuming your contentDetails actually contains:

<p>This is data sample.&nbsp;</p><p>Second part of the paragraph.&nbsp;</p>

($nbsp; replaced with &nbsp;)

Your problem is that when you call htmlentities on contentDetails it converts &nbsp; into &amp;nbsp;, so your str_replace won't find any matches. To solve the problem, call str_replace before htmlentities:

$xml .= '<content>' . htmlentities(str_replace("&nbsp;", "", $_POST['contentDetails'])) . '</content>';

Note that associative array keys should be enclosed in quotes; this will cause a warning now but in future PHP versions will be an error.

Upvotes: 1

Related Questions