


Summary of string processing skills in PHP introductory tutorial (conversion, filtering, parsing, search, interception, replacement, etc.)
本文实例总结了PHP字符串处理技巧。分享给大家供大家参考,具体如下:
Demo1.php
<?php //源代码是文本形式,页面显示是 web 形式 $str = ' PHP '; //清理一下两边的空格 ltrim 只清理左,rtrim只清理右边 echo ltrim($str); echo '<br/>'; echo rtrim($str); echo '<br/>'; //两边都清理 echo trim($str); //echo chop($str); ?>
Demo2.php
<?php $str = "This is 一站式建网站 \n This is a OneStopWeb"; //但是,我现在想要在网页中实现换行 //在回帖的时候,一个回车就是 \n //我们通过函数来实现转换过程 echo nl2br($str); ?>
Demo3.php
<?php //将所有字符转换成 HTML $str = '<strong>阅谁问君诵,水落清香浮。</strong>'; //echo htmlentities($str); //我们只要转换特殊字符即可 //echo htmlspecialchars($str);//<strong>阅谁问君诵,水落清香浮。</strong> echo strip_tags($str); //阅谁问君诵,水落清香浮。 ?>
Demo4.php
<?php $str = 'This is "一站式建网站" \n This is a OneStopWeb'; //对于即将插入数据库的字符串,把有问题的字符处理一下 //echo addslashes($str); //This is \"一站式建网站\" \\n This is a OneStopWeb $a = addslashes($str); //这个 $a 就是写入数据库的,我拿出来的话,就会有 \ 这个符号 echo stripcslashes($a); //首先将写进数据库的字符串通过 addslashes() 函数过滤一下,然后拿出来的时候 //再通过 stripcslashes() 解析一下显示 ?>
Demo5.php
<?php //将字符串转换成大写 echo strtoupper('oneStopWeb');//ONESTOPWEB //将字符串转换成小写 echo strtolower('oneStopWeb');//onestopweb //将第一个字母转换成大写 echo ucfirst('oneStopWeb'); //OneStopWeb ?>
Demo6.php
<?php $str = 'oneStopWeb'; //echo str_pad($str,11).'is good!'; //oneStopWeb is good! //oneStopWeb### //echo str_pad($str,13,'#') echo str_pad($str,18,'#',STR_PAD_BOTH); echo '<br/>'; echo str_pad($str,18,'#',STR_PAD_LEFT); echo '<br/>'; echo str_pad($str,18,'#',STR_PAD_RIGHT); /** * ####oneStopWeb#### * ########oneStopWeb * oneStopWeb######## * */ ?>
Demo7.php
<?php //explode -- 使用一个字符串分割另一个字符串 //返回的是一个数组 //explode 第一参数是分割字符串,第二个参数是要被分割的字符串 $email = explode('@','oneStopWeb@163.com'); //print_r($email); //Array ( [0] => oneStopWeb [1] => 163.com ) //分割完了之后,我经过一轮筛选,还要重新组合 // $arr = array('oneStop','@','163.com'); // $str = implode('&',$arr); // echo $str;//oneStop&@&163.com $str = implode(' - ',$email); echo $str; //oneStopWeb - 163.com ?>
Demo8.php
<?php // $str = 'I will be back'; // $arr = explode(' ',$str); // print_r($arr); //Array ( [0] => I [1] => will [2] => be [3] => back ) $str = 'I,will.be#back'; $tok = strtok($str,',.#'); //echo $tok; while($tok){ echo $tok.'<br/>'; $tok = strtok(',.#'); } // $str = 'I will be back'; // $tok = strtok($str,' '); // //echo $tok; // while($tok){ // echo $tok.'<br/>'; // $tok = strtok(' '); // } // $tok = strtok(' '); // echo $tok;//Iwill // //指针没有下移,而是重新来了一次 // $tok = strtok($str,' '); // echo $tok; ?>
Demo9.php
<?php $str = 'oneStopWeb@163.com'; //中间的参数表示开始的位置,位置是从 0 开始的,最后一个参数,是取出的个数 echo substr($str,0,5) ;//oneSt ?>
Demo10.php
<?php //$str = 'oneStopWeb@163.com'; $str = '阅谁问君诵,水落清香浮。'; //中文乱码 print_r(str_split($str)); ?>
Demo11.php
<?php $str = 'oneStopWeb@163.com'; echo strrev($str); //moc.361@beWpotSeno ?>
Demo12.php
<?php //通过 == 来比较字符串是否一致 //最后的返回值是布尔值 //echo 'a' == 'a'; //比较两个字符串 //echo strcmp('a','b'); // if(strcmp('a','a') == 0){ // echo '相等'; // } //echo strcasecmp('B','b'); //不区别大小写 //目前是非自然排序 //echo strcmp('2','10');//1 //如果按照自然排序方式比较呢? echo strnatcmp('2','10');//-1 ?>
Demo13.php
<?php //strspn //后面两个数字的参数,是从第几位开始,取多少位 echo strspn('one','oneStopWeb@163.com',1,5); ?>
Demo14.php
<?php //测试字符串的长度 echo strlen('oneStopWeb@163.com') ; ?>
Demo15.php
<?php //测试字符串出现的频率 echo substr_count('oneStopWeb@163.com','o'); ?>
Demo16.php
<?php //从指定的字符串开始输出之后的字符串 echo strstr('oneStopWeb@163.com','@'); //不区别大小写的 echo stristr('oneStopWeb@163.com','s'); ?>
Demo17.php
<?php //查找某字符串最先出现的位置 //位置是从第 0 个位置开始计算,W第一次出现在第 7 个位置上 echo strpos('oneStopWeb@163.com','W'); //最后出现的位置 echo strrpos('oneStopWeb@163.com','o'); ?>
Demo18.php
<?php //字符串替换 //第一个参数表示要查找的字符串(部分) //第二个参数表示要替换成的字符串(部分) //第三个参数表示原来的被替换的完整字符串 //echo str_replace('one','yi','oneStopWeb@163.com');//yiStopWeb@163.com //str_ireplace 是不区分大小写 //echo str_ireplace('oNe','yi','oneStopWeb@163.com');//yiStopWeb@163.com //从第一个位置开始(参数3),取出 5 个来(参数4),将它替换成 ###(参数2) echo substr_replace('oneStopWeb@163.com','###',0,5); ?>
Demo19.php
<?php //取中文长度 $str = '阅谁问君诵,水落清香浮。'; //用普通的 strlen 取一个中文字,就算两个 //echo strlen($str) ; //使用 mb_strlen 来取中文,有第二个参数,字符编码 echo mb_strlen($str,'GBK');//12 //如果你使用普通的 strlen 这个函数,我取 1 个字符 //采用 mb_substr 来取中文字符 echo mb_substr($str,2,1,'GBK');//问 ?>
Demo20.php
<?php $str = '阅谁问君诵,水落清香浮。'; //求出 o 最先出现的位置 //中文的算法,两个字符一个中文字 //mb 就算一个 //echo strpos($str,'问');//4 //按中文一个字符来计算,这样不会出现半个中文的状态 echo mb_strpos($str,'问',0,'GBK'); ?>
Demo21.php
<?php // $str = '阅谁问君诵,水落清香浮。' ; // echo mb_substr($str,0,1,'GBK'); $str ='阅谁问君诵,水落清香浮。'; //这个就无所谓用 mb_strstr echo strstr($str,'水'); ?>
Demo22.php
<?php $str ='阅谁问君诵,水落清香浮。'; echo mb_substr_count($str,'问','GBK'); ?>
希望本文所述对大家PHP程序设计有所帮助。
更多PHP入门教程之字符串处理技巧总结(转换,过滤,解析,查找,截取,替换等)相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

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,

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.

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.

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? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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�...

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.
