Home Backend Development PHP Tutorial Summary of string processing skills in PHP introductory tutorial (conversion, filtering, parsing, search, interception, replacement, etc.)

Summary of string processing skills in PHP introductory tutorial (conversion, filtering, parsing, search, interception, replacement, etc.)

Dec 22, 2016 pm 02:02 PM

本文实例总结了PHP字符串处理技巧。分享给大家供大家参考,具体如下:

Demo1.php

<?php
  //源代码是文本形式,页面显示是 web 形式
  $str = &#39;      PHP      &#39;;
  //清理一下两边的空格 ltrim 只清理左,rtrim只清理右边
  echo ltrim($str);
  echo &#39;<br/>&#39;;
  echo rtrim($str);
  echo &#39;<br/>&#39;;
  //两边都清理
  echo trim($str);
  //echo chop($str);
?>
Copy after login

Demo2.php

<?php
  $str = "This is 一站式建网站 \n This is a OneStopWeb";
  //但是,我现在想要在网页中实现换行
  //在回帖的时候,一个回车就是 \n
  //我们通过函数来实现转换过程
  echo nl2br($str);
?>
Copy after login

Demo3.php

<?php
  //将所有字符转换成 HTML
  $str = &#39;<strong>阅谁问君诵,水落清香浮。</strong>&#39;;
  //echo htmlentities($str);
  //我们只要转换特殊字符即可
  //echo htmlspecialchars($str);//<strong>阅谁问君诵,水落清香浮。</strong>
  echo strip_tags($str); //阅谁问君诵,水落清香浮。
?>
Copy after login

Demo4.php

<?php
  $str = &#39;This is "一站式建网站" \n This is a OneStopWeb&#39;;
  //对于即将插入数据库的字符串,把有问题的字符处理一下
  //echo addslashes($str); //This is \"一站式建网站\" \\n This is a OneStopWeb
  $a = addslashes($str);
  //这个 $a 就是写入数据库的,我拿出来的话,就会有 \ 这个符号
  echo stripcslashes($a);
  //首先将写进数据库的字符串通过 addslashes() 函数过滤一下,然后拿出来的时候
  //再通过 stripcslashes() 解析一下显示
?>
Copy after login

Demo5.php

<?php
  //将字符串转换成大写
  echo strtoupper(&#39;oneStopWeb&#39;);//ONESTOPWEB
  //将字符串转换成小写
  echo strtolower(&#39;oneStopWeb&#39;);//onestopweb
  //将第一个字母转换成大写
  echo ucfirst(&#39;oneStopWeb&#39;); //OneStopWeb
?>
Copy after login

Demo6.php

<?php
  $str = &#39;oneStopWeb&#39;;
  //echo str_pad($str,11).&#39;is good!&#39;; //oneStopWeb is good!
  //oneStopWeb###
  //echo str_pad($str,13,&#39;#&#39;)
  echo str_pad($str,18,&#39;#&#39;,STR_PAD_BOTH);
  echo &#39;<br/>&#39;;
  echo str_pad($str,18,&#39;#&#39;,STR_PAD_LEFT);
  echo &#39;<br/>&#39;;
  echo str_pad($str,18,&#39;#&#39;,STR_PAD_RIGHT);
/**
 * ####oneStopWeb####
 * ########oneStopWeb
 * oneStopWeb########
 * */
?>
Copy after login

Demo7.php

<?php
  //explode -- 使用一个字符串分割另一个字符串
  //返回的是一个数组
  //explode 第一参数是分割字符串,第二个参数是要被分割的字符串
  $email = explode(&#39;@&#39;,&#39;oneStopWeb@163.com&#39;);
  //print_r($email); //Array ( [0] => oneStopWeb [1] => 163.com )
  //分割完了之后,我经过一轮筛选,还要重新组合
// $arr = array(&#39;oneStop&#39;,&#39;@&#39;,&#39;163.com&#39;);
// $str = implode(&#39;&&#39;,$arr);
// echo $str;//oneStop&@&163.com
  $str = implode(&#39; - &#39;,$email);
  echo $str; //oneStopWeb - 163.com
?>
Copy after login

Demo8.php

<?php
// $str = &#39;I will be back&#39;;
// $arr = explode(&#39; &#39;,$str);
// print_r($arr); //Array ( [0] => I [1] => will [2] => be [3] => back )
  $str = &#39;I,will.be#back&#39;;
  $tok = strtok($str,&#39;,.#&#39;);
  //echo $tok;
  while($tok){
    echo $tok.&#39;<br/>&#39;;
    $tok = strtok(&#39;,.#&#39;);
  }
// $str = &#39;I will be back&#39;;
// $tok = strtok($str,&#39; &#39;);
// //echo $tok;
// while($tok){
//   echo $tok.&#39;<br/>&#39;;
//   $tok = strtok(&#39; &#39;);
// }
// $tok = strtok(&#39; &#39;);
// echo $tok;//Iwill
// //指针没有下移,而是重新来了一次
// $tok = strtok($str,&#39; &#39;);
// echo $tok;
?>
Copy after login

Demo9.php

<?php
  $str = &#39;oneStopWeb@163.com&#39;;
  //中间的参数表示开始的位置,位置是从 0 开始的,最后一个参数,是取出的个数
  echo substr($str,0,5)  ;//oneSt
?>
Copy after login

Demo10.php

<?php
  //$str = &#39;oneStopWeb@163.com&#39;;
  $str = &#39;阅谁问君诵,水落清香浮。&#39;; //中文乱码
  print_r(str_split($str));
?>
Copy after login

Demo11.php

<?php
  $str = &#39;oneStopWeb@163.com&#39;;
  echo strrev($str); //moc.361@beWpotSeno
?>
Copy after login

Demo12.php

<?php
  //通过 == 来比较字符串是否一致
  //最后的返回值是布尔值
  //echo &#39;a&#39; == &#39;a&#39;;
  //比较两个字符串
  //echo strcmp(&#39;a&#39;,&#39;b&#39;);
// if(strcmp(&#39;a&#39;,&#39;a&#39;) == 0){
//   echo &#39;相等&#39;;
// }
  //echo strcasecmp(&#39;B&#39;,&#39;b&#39;); //不区别大小写
  //目前是非自然排序
  //echo strcmp(&#39;2&#39;,&#39;10&#39;);//1
  //如果按照自然排序方式比较呢?
  echo strnatcmp(&#39;2&#39;,&#39;10&#39;);//-1
?>
Copy after login

Demo13.php

<?php
  //strspn
  //后面两个数字的参数,是从第几位开始,取多少位
  echo strspn(&#39;one&#39;,&#39;oneStopWeb@163.com&#39;,1,5);
?>
Copy after login

Demo14.php

<?php
  //测试字符串的长度
  echo strlen(&#39;oneStopWeb@163.com&#39;) ;
?>
Copy after login

Demo15.php

<?php
  //测试字符串出现的频率
  echo substr_count(&#39;oneStopWeb@163.com&#39;,&#39;o&#39;);
?>
Copy after login

Demo16.php

<?php
  //从指定的字符串开始输出之后的字符串
  echo strstr(&#39;oneStopWeb@163.com&#39;,&#39;@&#39;);
  //不区别大小写的
  echo stristr(&#39;oneStopWeb@163.com&#39;,&#39;s&#39;);
?>
Copy after login

Demo17.php

<?php
  //查找某字符串最先出现的位置
  //位置是从第 0 个位置开始计算,W第一次出现在第 7 个位置上
  echo strpos(&#39;oneStopWeb@163.com&#39;,&#39;W&#39;);
  //最后出现的位置
  echo strrpos(&#39;oneStopWeb@163.com&#39;,&#39;o&#39;);
?>
Copy after login

Demo18.php

<?php
  //字符串替换
  //第一个参数表示要查找的字符串(部分)
  //第二个参数表示要替换成的字符串(部分)
  //第三个参数表示原来的被替换的完整字符串
  //echo str_replace(&#39;one&#39;,&#39;yi&#39;,&#39;oneStopWeb@163.com&#39;);//yiStopWeb@163.com
  //str_ireplace 是不区分大小写
  //echo str_ireplace(&#39;oNe&#39;,&#39;yi&#39;,&#39;oneStopWeb@163.com&#39;);//yiStopWeb@163.com
  //从第一个位置开始(参数3),取出 5 个来(参数4),将它替换成 ###(参数2)
  echo substr_replace(&#39;oneStopWeb@163.com&#39;,&#39;###&#39;,0,5);
?>
Copy after login

Demo19.php

<?php
  //取中文长度
  $str = &#39;阅谁问君诵,水落清香浮。&#39;;
  //用普通的 strlen 取一个中文字,就算两个
  //echo strlen($str) ;
  //使用 mb_strlen 来取中文,有第二个参数,字符编码
  echo mb_strlen($str,&#39;GBK&#39;);//12
  //如果你使用普通的 strlen 这个函数,我取 1 个字符
  //采用 mb_substr 来取中文字符
  echo mb_substr($str,2,1,&#39;GBK&#39;);//问
?>
Copy after login

Demo20.php

<?php
  $str = &#39;阅谁问君诵,水落清香浮。&#39;;
  //求出 o 最先出现的位置
  //中文的算法,两个字符一个中文字
  //mb 就算一个
  //echo strpos($str,&#39;问&#39;);//4
  //按中文一个字符来计算,这样不会出现半个中文的状态
  echo mb_strpos($str,&#39;问&#39;,0,&#39;GBK&#39;);
?>
Copy after login

Demo21.php

<?php
// $str = &#39;阅谁问君诵,水落清香浮。&#39;  ;
// echo mb_substr($str,0,1,&#39;GBK&#39;);
  $str =&#39;阅谁问君诵,水落清香浮。&#39;;
  //这个就无所谓用 mb_strstr
  echo strstr($str,&#39;水&#39;);
?>
Copy after login

Demo22.php

<?php
  $str =&#39;阅谁问君诵,水落清香浮。&#39;;
  echo mb_substr_count($str,&#39;问&#39;,&#39;GBK&#39;);
?>
Copy after login

   

希望本文所述对大家PHP程序设计有所帮助。

更多PHP入门教程之字符串处理技巧总结(转换,过滤,解析,查找,截取,替换等)相关文章请关注PHP中文网!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles