Method: 1. Use str_replace(), the syntax "str_replace(" "," ",$str)"; 2. Use preg_replace() with regular expressions, the syntax "preg_replace('/\s / '," ",$str)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
nbspSpace character
In HTML, is a space entity, also known as a non-breaking space. It is the most common space that we use the most. Most people may only be exposed to it. It is a space generated by pressing the space key. . In HTML, if you use the space bar to generate this space, the spaces will not accumulate (only count as 1). You need to use html entity representation to accumulate,
php method to convert spaces into nbsp characters
Use str_replace () Function
Use preg_replace() function
1. Use str_replace() function
"; $newStr = str_replace(" ", " ", $str); echo $newStr; ?>
2. Use the preg_replace() function
Use the preg_replace() function with the regular expression "/\s /
" to replace.
"; $newStr = preg_replace('/\s+/', " ", $str); echo $newStr; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert spaces into nbsp characters in php. For more information, please follow other related articles on the PHP Chinese website!