Home  >  Article  >  PHP string function (5): delete or fill specified content

PHP string function (5): delete or fill specified content

无忌哥哥
无忌哥哥Original
2018-06-28 10:49:563804browse

* 1.ltrim($str,$mask): Delete spaces or specified characters from the left

* 2.rtrim($str,$mask): Delete spaces or specified characters from the right

* 3.trim($str,$mask): Remove spaces or specified characters from the left and right edges

* 4.str_pad($str,$length,$mark,CONST): Use specific characters to Fill the string to the specified length

* You can use three constants to specify the direction: STR_PAD_LEFT/STR_PAD_RIGHT/STR_PAD_BOTH, and use spaces by default

* 5.chunk_split($str, $length,[$end ]): Cut the string into small pieces according to size, you can specify the delimiter

$str = '  php中文网   '; //左边二个空格,右边三个空格
$str1 = 'www.php.cn';
echo $str,&#39;<br>&#39;;
echo strlen($str),&#39;<br>&#39;;
echo &#39;<hr color="red">&#39;;

//1.ltrim($str,$mask): Remove spaces from the left or specify the character

echo ltrim($str),&#39;<br>&#39;;
echo strlen(ltrim($str)),&#39;<br>&#39;;
echo $str1,&#39;<br>&#39;;
echo strlen($str1),&#39;<br>&#39;;
echo ltrim($str1, &#39;www.&#39;),&#39;<br>&#39;;
echo strlen(ltrim($str1, &#39;www.&#39;)),&#39;<br>&#39;;

//2.rtrim($str,$mask): Delete spaces or specified characters from the right side

$str = &#39;  php中文网   &#39;;
echo rtrim($str),&#39;<br>&#39;;
echo strlen(rtrim($str)),&#39;<br>&#39;;

//3.trim($str,$mask): Delete spaces or specified characters from the left and right sides

$str = &#39;  php中文网   &#39;;
echo trim($str),&#39;<br>&#39;;
echo strlen(trim($str)),&#39;<br>&#39;;
echo &#39;<hr>&#39;;

//4.str_pad($str,$length,$mark,CONST): Use specific characters to pad the string to the specified length

$str1 = &#39;www.php.cn&#39;;
echo strlen($str1),&#39;<br>&#39;;
echo str_pad($str1, 20, &#39;*&#39;, STR_PAD_RIGHT),&#39;<br>&#39;; //默认向右填充
echo str_pad($str1, 20, &#39;*&#39;, STR_PAD_LEFT), &#39;<br>&#39;; //向左填充
echo str_pad($str1, 20, &#39;*&#39;, STR_PAD_BOTH), &#39;<br>&#39;; //二边填充
echo &#39;<hr>&#39;;

//5.chunk_split($str, $length,[$end]): Cut the string into small pieces according to size, you can specify the separator

$str1 = &#39;12345678901234567890&#39;;
echo chunk_split($str1, 7, &#39;,&#39;),&#39;<br>&#39;;
echo chunk_split($str1, 7, &#39;<br>&#39;);
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