Home  >  Article  >  Backend Development  >  Summary of Xiaopang learning PHP 4-----PHP string operations_PHP tutorial

Summary of Xiaopang learning PHP 4-----PHP string operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:46:47998browse

Summary of Xiaopang learning PHP 4-----String operations of PHP

1. String concatenation

Strings are connected through a half-width period ".", and two or more strings can be connected into one string.

2. Remove leading and trailing spaces and special characters from the string

PHP provides the trim() function to remove spaces and special characters on the left and right sides of a string, the ltrim() function to remove spaces and special characters on the left side of a string, and the rtrim() function to remove spaces and special characters on the right side of a string.
<!--?php
    //去除特殊字符串
    $str = "\r\r(:@_@ 闯世界 哈哈哈 @_@:)";
    echo trim($str).&#39;<br-->&#39;;  //去除所有的特殊字符
    echo trim($str,"\r\r(::)").&#39;
&#39;;    //去除制定的特殊字符
    echo ltrim($str,"\r\r").&#39;
&#39;;     //去除左边制定的特殊字符
    echo rtrim($str,"@_@").&#39;
&#39;;    //去除右边制定的特殊字符
?>

3. Escape and restore string data

There are two types of string escaping and restoration: the first is to manually escape and restore string data, and the second is to automatically escape and restore string data. For manual escaping, you just need to add a backslash "". Let's talk about the automatic escaping function: addslashes(string) adds escape characters to the string, and the addcslashes(string charlist) function implements the string. The characters specified in are escaped, and stripslashes() is used to restore the string escaped by the two functions above.
<!--?php
    //自动转义、还原字符串
    $str1 = "select * from tb_book WHERE bookName = &#39;PHP5从开发到入门&#39;";
    $a = addslashes($str1);   //自动转义所有的字符串
    $c = addcslashes($str1,"from");   //转义特定的字符串
    echo &#39;$a转义后的字符串:&#39;.$a.&#39;<br-->&#39;;
    echo &#39;$str1转义前的字符串:&#39;.$str1.&#39;
&#39;;
    echo &#39;$c转义特定的字符串:&#39;.$c.&#39;
&#39;;
    $b = stripcslashes($str1);
    echo &#39;$b还原后的字符串:&#39;.$b.&#39;
&#39;;
    echo &#39;$a还原前的字符串:&#39;.$a.&#39;
&#39;;
    echo &#39;

'; ?>

4. Get the string length, intercept the string, and perform string comparison

<!--?php
    //字符串比较函数
    $str2 = "my name is haogaoming";
    echo &#39;$str2的字符串长度为:&#39;.strlen($str2).&#39;<br-->&#39;;   //字符串的长度
    echo substr($str2,0).&#39;
&#39;;        //从0位置截取全部长度字符串
    echo substr($str2,4,10).&#39;
&#39;;     //从第四个位置截取字符串,包含第四个位置,位置是从下标0开始
    echo substr($str2,-4,4).&#39;
&#39;;     //从倒数第四个位置截取,不包含第四个位置
    echo substr($str2,0,-4).&#39;
&#39;;     //从第0位置截取,一直到最后第四位,包含第四位
    //两个比较字符串函数 :strcmp(区分大小写),strcasecmp(不区分大小写);strncmp(比较两个字符串中前几个字符串是否一样);
    //strstr(检索字符串,从这个字符串首次出现的位置到末尾的字符);strchr(从字符串后序的位置开始检索字符串,和前者相反);
    //substr_count(检索特定的字符串在某一个字符串中出现的次数)

    //替换函数:str_ireplace(search,replace,subject,int &count)查找某一串字符串然后进行替换,search代表了查找需要替换的字符串,replace代表了要替换的值
    //subject代表了整个字符串,count代表了替换工作执行了多少次,这个函数不区分大小写,如果要区分大小写就用str_replace函数

    //替换函数:substr_replace(string,replace,start,length)对string字符串从start开始到length之间的字符串替换为replace
    echo &#39;

'; ?>

5. Format string

<!--?php
    //格式化数字字符串
    $num = 118888.8321212;
    echo number_format($num).&#39;<br-->&#39;;
    echo number_format($num,2).&#39;
&#39;;
    echo number_format($num,2,&#39;,&#39;,&#39;.&#39;).&#39;
&#39;;
    //分割字符串
    $str3 = &#39;PHP 编程词典@NET 编程字典@ASP 编程字典@JSP 编程字典&#39;;
    $str_array = explode(&#39;@&#39;,$str3);
    echo &#39;拆分的字符串为:&#39;;
    print_r($str_array);
    echo &#39;
&#39;;
    //合成字符串
    echo &#39;合成字符串函数:&#39;;
    echo implode("合成",$str_array).&#39;
&#39;;
?>


Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1030447.htmlTechArticleXiao Pang learns PHP summary 4-----PHP string operations 1. String connection string is Concatenated by half-width period., two or more strings can be concatenated into one string. ...
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