Home  >  Article  >  Backend Development  >  How to convert spaces into nbsp characters in php

How to convert spaces into nbsp characters in php

青灯夜游
青灯夜游Original
2022-04-25 20:30:342952browse

Method: 1. Use str_replace(), the syntax "str_replace(" "," ",$str)"; 2. Use preg_replace() with regular expressions, the syntax "preg_replace('/\s / '," ",$str)".

How to convert spaces into nbsp characters in php

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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str= &#39;This is a programming tutorial !&#39;;
echo $str."<br>";
$newStr = str_replace(" ", " ", $str); 
echo $newStr;
?>

How to convert spaces into nbsp characters in php

2. Use the preg_replace() function

Use the preg_replace() function with the regular expression "/\s / " to replace.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str= &#39; 欢迎 来到 PHP中文网 !&#39;;
echo $str."<br>";
$newStr = preg_replace(&#39;/\s+/&#39;, " ", $str); 
echo $newStr;
?>

How to convert spaces into nbsp characters in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn