PHP str_replace function problem

In summary, the PHP code provided is used to edit HTML text to be displayed as standard text. However, there is an issue with the code when replacing multiple spaces with  . The potential solution is to use " " instead. It is also suggested to consider using the strip_tags() function.
  • #1
chrisalviola
80
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
  • #2
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;".
 
  • #3
... just curious, are you aware of the strip_tags() function?
 
  • #4
Sometime in the last nine'n'half years he/she may have figured it out :wink:
 
  • #5
These threads get hundreds to thousands of views from search engines over the years. Replies are not just helping the original poster.
 
  • Like
Likes hmmm27

Related to PHP str_replace function problem

1. What is the purpose of the PHP str_replace function?

The str_replace function is used to replace all occurrences of a specified string in a given text with a different string.

2. How do I use the str_replace function in my code?

To use the str_replace function, you must specify three parameters: the string to be replaced, the replacement string, and the text or variable where the replacement will occur. You can also specify an optional fourth parameter to limit the number of replacements.

3. Can I use the str_replace function to replace multiple strings at once?

Yes, you can use the str_replace function to replace multiple strings at once by passing in arrays for the first two parameters instead of just single strings. The function will then replace each string in the first array with its corresponding replacement in the second array.

4. What happens if the string to be replaced is not found in the text?

If the string to be replaced is not found, the str_replace function will simply return the original text without making any replacements.

5. Are there any alternative functions to str_replace for string replacements?

Yes, there are several alternative functions for string replacements in PHP, such as preg_replace, strtr, and substr_replace. Each function has its own specific use cases and syntax, so it's important to read the documentation and choose the right one for your needs.

Similar threads

  • Programming and Computer Science
Replies
32
Views
2K
Replies
1
Views
3K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Replies
1
Views
2K
Back
Top