백엔드 개발 PHP 튜토리얼 PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)_PHP

PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)_PHP

May 28, 2016 am 11:49 AM
php PHP 문자열 함수 기능

本文实例总结了PHP常用字符串操作函数。分享给大家供大家参考,具体如下:

/*常用的字符串输出函数
*
* echo() 输出字符串
* print() 输出一个或多个字符串
* die() 输出一条信息,并退出当前脚本
* printf() 输出格式化字符串
* sprintf() 把格式化的字符串写入到一个变量中
*
*/
//ucfirst 
//将字符串中的首字母转换为大写
$str="string";
echo ucfirst($str);
echo "<hr><br/>";
//ucwords()
//将字符串中的每个单词的首字母大写
$ucword="hello everyone!";
echo ucwords($ucword);
echo "<hr><br/>";
//ltrim() rtrim() trim()
//去除空格
$str="123 This is a test.....";
echo ltrim($str,"0..9")."<br/>"; //去除左侧的数字 
echo rtrim($str,".")."<br/>";
echo trim($str,"0..9A..Z.")."<br/>"; //去除字符串两端的大写字母,数字还有.
//HTML相关的字符串格式化函数
//nl2br()
//将字符串中的\n转换为"<br/>"
$str="this is \n hello world";
echo nl2br($str).'<br/>';
//htmlspecialchars()
//将html标记以字符的形式显示,不进行解释
$str="<b>hello world</b>";
echo $str."<br/>";
echo htmlspecialchars($str);
echo "<hr><br/>";
//addcslashes
//添加反斜线
$str=addcslashes("foo[]","A..z");
echo $str."<br/>";
echo addcslashes("zoo['.']",'A..z')."<br/>";
//convert_uuencode()
//利用uudecode的方法对字符串进行编码
$string="hello world";
$str= convert_uuencode($string);
echo $str."<br/>";
echo convert_uudecode($str)."<br/>";
//html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' ]] )
//与htmlentities方法相反,将进行编码后的html字符转换为浏览器能够编译的形式
$a="I want a bright <b>future</b>";
$b= htmlentities($a)."<br/>";
echo $b;
echo html_entity_decode($b);
echo "<hr><br/>";
//htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
//与htmlspecialchars函数相反,将HTML实体转换为字符
$c=htmlspecialchars($a);
echo $c."<br/>";
echo htmlspecialchars_decode($c)."<br/>";
echo "<hr><br/>";
//lcfirst ( string $str )
//将字符串的首字符小写
$str="Hello World";
// echo lcfirst($str)."<br/>";
//md5_file ( string $filename [, bool $raw_output = false ] )
//对文件进行md5加密
//
$string="password";
$str=md5($string);
if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){
 echo "The password is right <br/>";
}
//parse_str ( string $str [, array &$arr ] )
//将一个字符串进行解析,解析成变量和数组的形式
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str,$input);
print_r($input);
echo "<hr><br/>";
//string sha1_file ( string $filename [, bool $raw_output = false ] )
//计算文件的散列值
foreach(glob("C:/lamp/appache2/htdocs/*.php") as $ent){
 if(is_dir($ent)){
 continue;
 }
 echo $ent."(SHA1:".sha1_file($ent).")<br/>";
}
echo "<hr><br/>";
//int similar_text ( string $first , string $second [, float &$percent ] )
//计算两个字符串的相似度,通过引用方式传递第三个参数,similar_text() 将
//计算相似程度百分数。
$string1="rogerzhalili";
$string2="zhangjieroger";
if(similar_text($string1,$string2,$percent)){
 echo $string1." and ".$string2." has the similarity of:".$percent."<br/>";
}
echo "<hr><br/>";
//string str_shuffle ( string $str )
//打乱一个字符串
$string="I want you to solve this problem";
echo str_shuffle($string)."<br/>";
//array str_split ( string $string [, int $split_length = 1 ] )
//按照指定的长度对字符串进行分割
$arr=str_split($string,3);
//str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
//统计字符串中单词的数量
echo "<hr><br/>";
//int strripos ( string $haystack , string $needle [, int $offset = 0 ] )
//以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位
//置。与 strrpos() 不同,strripos() 不区分大小写。
//offset用于指定从那个位置开始查找
$haystack='ababcd';
$needle='Ab';
echo "the last".$needle."postion is:".strripos($haystack,$needle)."<br/>";
echo strrpos($haystack,'ab');
echo "<hr><br/>";
//string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
//返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结
//尾的字符串。 该函数区分大小写。如果想要不区分大小写,请使用
//stristr()。
$a="the First test";
$needle="Fi";
echo strstr($a,$needle)."<br/>";
if($c=strstr($a,"Fio")){
 echo "find".$c."<br/>";
}
else
{
 echo "not find the string!<br/>";
}
echo "<hr><br/>";
//int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
//查找$needle子字符串在$haystack中出现的次数,$needle区分大小写
$hay="la la wa la wa wa lala";
echo substr_count($hay,"la")."<br>";
//int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
//正则匹配,将匹配后的结果存放到$matches(如果指定了$matches的话)
preg_match_all("/&#63;(\d3)&#63;&#63; (&#63;(1) [\-\s] ) \d{3}-\d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
echo "<pre class="brush:php;toolbar:false">";
print_r($phones);
echo "
로그인 후 복사
"; echo "

"; //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) //搜索subject中匹配pattern的部分, 以replacement进行替换. $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}1,$3'; echo preg_replace($pattern,$replacement,$string); echo "

"; //array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] ) //通过一个正则表达式分隔给定字符串. $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars);

更多关于PHP字符串操作相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结》

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

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

See all articles