PHP function to delete left and right spaces_PHP tutorial

WBOY
Release: 2016-07-20 10:59:15
Original
908 people have browsed it

The deletion function in PHP is much more specific than that in JS. In addition to the trim() function, there are also the ltrim() and rtrim() functions. They need to delete the spaces around and around respectively. In addition to these three functions, you can also use regular deletion. ​

ltrim() function

ltrim($str, $charlist)
//$str represents the processed string;
//$charlist is the special character to be deleted. If it is empty, remove the space on the left

The code is as follows Copy code
 代码如下 复制代码

 $t=" ...I'm Jacky...";
 echo "a" .$t ."
";
 $left=ltrim($t);
 echo "a" .$left ."
";
 $lleft=ltrim($left,".");
 echo $lleft;
?>

$t=" ...I'm Jacky...";

echo "a" .$t ."

";

$left=ltrim($t);
echo "a" .$left ."
";
$lleft=ltrim($left,".");

echo $lleft;
 代码如下 复制代码

 $a="htm ";
 echo $a ."l" ."
";
 echo rtrim($a) ."l";
?>

?>

rtrim() function

 代码如下 复制代码

$str = ” This line containstliberal rn use of   whitespace.nn”;

// 首先去掉头尾空格
$str = trim($str);

// 接着去掉两个空格以上的


$str = preg_replace(’/s(?=s)/’, ‘’, $str);

// 最后将非空格替换为一个空格
$str = preg_replace(’/[nrt]/’, ‘ ‘, $str);

rtrim($str, $charlist)

//$str represents the processed string;

//$charlist is the special character to be deleted. If it is empty, remove the space on the right

The code is as follows Copy code

$a="htm "; echo $a ."l" ."
 代码如下 复制代码

$str="     一聚教程网 www.bkjia.com     ";
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace('  ', "n  ", $str);
?>

";
echo rtrim($a) ."l"; ?>

trim() functionFirst remove the leading and trailing spaces
The code is as follows Copy code
$str = ” This line containstliberal rn use of whitespace.nn”; // First remove the leading and trailing spaces $str = trim($str); // Then remove more than two spaces $str = preg_replace(’/s(?=s)/’, ‘’, $str); //Finally replace the non-space with a space $str = preg_replace(’/[nrt]/’, ‘ ‘, $str);
Use the example above to remove all extra spaces. First use TRim() to remove leading and trailing spaces, then use preg_replace() to remove repeated spaces Replace with regular expressions, more powerful PHP removes leading and trailing spaces from strings (including full-width)
The code is as follows Copy code
$str="    Yiju Tutorial Network www.bkjia.com  "; $str = mb_ereg_replace('^( | )+', '', $str); $str = mb_ereg_replace('( | )+$', '', $str); echo mb_ereg_replace(' ', "n ", $str); ?>
http://www.bkjia.com/PHPjc/445628.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445628.htmlTechArticleDeleting functions in php is much more specific than js. In addition to the trim() function, there are also ltrim() and rtrim () function, they need to delete the spaces around and around respectively. In addition to these three functions, you can also use the positive...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template