Home >Backend Development >PHP Problem >How to delete the first space in a string in php

How to delete the first space in a string in php

藏色散人
藏色散人Original
2021-07-15 10:58:411505browse

php method to delete the first space in a string: first create a PHP sample file; then define a string containing spaces; finally delete the white space characters on the left side of the string through the ltrim function.

How to delete the first space in a string in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to delete the first string in php Space?

In PHP, you can use the ltrim() function to remove blank characters or other predefined characters on the left side of a string.

Syntax

ltrim(string,charlist)

Example

Remove spaces on the left side of the string:

<?php
$str = " Hello World!";
echo "不使用 ltrim: " . $str;
echo "<br>";
echo "使用 ltrim: " . ltrim($str);
?>

HTML output of the above code (please view the source code):

<!DOCTYPE html>
<html>
<body>
不使用 ltrim:    Hello World!<br>使用 ltrim: Hello World!
</body>
</html>

Browser output of the above code:

不使用 ltrim: Hello World!
使用 ltrim: Hello World!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete the first space in a string 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