PHP str_replace function problem

  • Thread starter Thread starter chrisalviola
  • Start date Start date
  • Tags Tags
    Function Php
Click For Summary

Discussion Overview

The discussion revolves around a PHP code snippet intended to format HTML text for display as standard text, specifically addressing issues with replacing single and multiple spaces with non-breaking spaces using the str_replace function.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a PHP function that uses str_replace to convert HTML characters and spaces into their respective HTML entities, raising a concern about replacing multiple spaces with  .
  • Another participant claims they cannot reproduce the issue, demonstrating that str_replace works for multiple spaces when tested.
  • A third participant suggests the use of the strip_tags() function, implying it may be relevant to the problem at hand.
  • A later reply humorously notes that the original poster may have resolved the issue over time.
  • Another participant emphasizes the value of the discussion for future readers, indicating that responses may assist others beyond the original poster.

Areas of Agreement / Disagreement

There is no consensus on the original poster's issue with multiple spaces, as one participant claims it works correctly while another raises the problem. The discussion remains unresolved regarding the effectiveness of the proposed solutions.

Contextual Notes

Participants have not clarified the context in which the original code is executed, nor have they addressed potential dependencies on browser behavior or HTML rendering that may affect the display of  .

chrisalviola
Messages
80
Reaction score
0
I have this PHP code to edit all HTML text to be displayed so that it won't appear as HTML but as standard text.

<?php
function htmlformat($sinput){

$newphrase = str_replace("<", "&lt;", $sinput);
$newphrase = str_replace(">", "&gt;", $newphrase);
$newphrase = str_replace(chr(13),"<br>",$newphrase);
$newphrase = str_replace(chr(34), "&quot;", $newphrase);
$newphrase = str_replace(chr(32), "&nbsp;",$newphrase);$newphrase=str_replace("","<font color=red>",$newphrase);

$newphrase=str_replace("","</font>",$newphrase);

$newphrase=str_replace("","<img src=",$newphrase);

$newphrase=str_replace("",">",$newphrase);return $newphrase;
}
?>the problem is this part of the code

$newphrase = str_replace(chr(32), "&nbsp;",$newphrase);it only replace the single spaces with &nbsp;
but what about multiple spaces?
when I have 2 more spaces on the text it can't seem to replace it with &nbsp;
 
Technology news on Phys.org
I can't reproduce that problem, works fine for me:
Code:
$newphrase = "a  b";
$newphrase = str_replace(chr(32), "&nbsp;",$newphrase);
              
echo $newphrase;

a&nbsp;&nbsp;b

If you use this as HTML code then a browser will convert these &nbsp; back to spaces and then display only one. If you want to avoid that, use "&amp;nbsp;".
 
... just curious, are you aware of the strip_tags() function?
 
Sometime in the last nine'n'half years he/she may have figured it out :wink:
 
These threads get hundreds to thousands of views from search engines over the years. Replies are not just helping the original poster.
 
  • Like
Likes   Reactions: hmmm27

Similar threads

  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 15 ·
Replies
15
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K