字符串操作
1.手动转移字符串数据- <?php
- $name='Garfield';
- $str1='The'.$name.' cat\'s is pretty \n';
- echo $str1;
- $str2="This is one line.\nAnd this's another line.";
- echo $str2;
- $var=123;
- $str3="The \$var is $var";
- echo $str3;
- ?>
- <?php
- $str="[sql]Who's Raymond?";
- echo $str . "This is not safe in a database query.<br/>";
- echo addslashes($str) . "This is safe in a database query."."<br/>";
- ?>
- <?php
- $str="Hello Friend,Never give up!";
- echo $str."<br/>";
- echo addcslashes($str, 'g')."<br/>";
- echo addcslashes($str, 'a..g')."<br/>";
- ?>
- <?php
- $var=98;
- echo chr($var);
- ?>
- <?php
- $str="hello";
- if (ord($str)==10){
- echo "\$str第一个字符的ASCII码是10.\n";
- }else{
- echo "\$str第一个字符的ASCII码是".ord($str);
- }
?>
- <?php
- $s='php';
- echo serialize($s);
- ?>
- ----------------------
- <?php
- $arry=array("cats","drop","fur","everywhere");
- echo "原数组内容:<pre>";
- print_r($arry);
- echo "<pre>序列化后的数组:<br/>";
- $string2=serialize($arry);
- print_r($string2);
- echo "</pre>反序列化后的数组:<br/><pre>";
- $arry2=unserialize($string2);
- print_r($arry2);
- echo "</pre>";
- ?>
- <?php
- $string="Hello\nWorld\n\nHow are you?";
- echo nl2br($string)."<br>"; //將/n转换为浏览器识别的<br/>换行符
- $textblock="See spot run, run spot run. See spot roll, run spot roll!";
- echo wordwrap($textblock,20,"<br/>");//在一行的第20个字符处强制换行
- ?>
- <?php
- $astring="heloo world";
- echo strtolower($astring);//將字符串全部转为小写字符
- echo strtoupper($astring);//將字符串全部转为大写字符
- echo ucfirst($astring);//將首字符大写
- echo ucwords($astring);//转换单词首字符大写
- ?>
- <?php
- $email="webmster@php.net";
- $mail_array=explode("@",$email);//以@为标识进行切分
- print_r($mail_array);
- $url_array=explode("/", $_SERVER['REQUEST_URI']);
- print_r($url_array);
- ?>
- <?php
- $test="Your programing ability is excellent";
- echo substr($test, 1)."<br>";//將得到"our programing ability is excellent"
- echo substr($test, -9)."<br>";//將得到"excellent"
- echo substr($test, 0,4)."<br>";//截取到第四个字符
- echo substr($test, 4,-13)."<br>";//从第四个字符串开始截取到倒数第13个字符
- ?>
- <?php
- echo strlen("China is great"); //注意空格也算一个字符
- echo strlen("伟大的中国");//一个汉字占用两个字符长度,返回10
- ?>
- <?php
- $text=<<<text
- 测试:programing is art! wahaha
- text;
- $words=str_word_count($text);
- echo "单词总数为:$words";
- ?>
- -------------------------------------
- <?php
- $text=<<<text
- To be pure PHP developer, PHP is simple,
- PHP is good web script.
- building our web site.
- text;
- $words=str_word_count($text,2);
- $count_array=array_count_values($words); //使用array_count_values对$words
- 作统计后生成一个新数组
- print_r($count_array);
- ?>
- 格式:strstr($string,"keyword");
- <?php
- $string="this is a demo string";
- if (strstr($string, "demo")){
- echo "有该子串";
- }else {
- echo "没有该子串";
- }
- ?>
- <?php
- $repeat_str=str_repeat("PHP5", 10);
- echo $repeat_str."<br>";
- ?>
- <?php
- $repeat_str=str_repeat("PHP5", 10);
- $counter=substr_count($repeat_str, "PHP5");
- echo "字符串中PHP5字符一共出现".$counter."次.";
- ?>
- <?php
- $text_1="Welcome to Beijing";
- $text_2="China";
- $text_3=str_replace("Beijing", $text_2, $text_1);
- echo $text_3;
- ?>
- <?php
- $string="abcdefg12345";
- echo strrev($string);
- ?>
- <?php
- $str='<a href="http://www.google.cn" title="Goole China">Goole 中国</a>';
- echo htmlspecialchars($str);
- ?>
- <?php
- $string="Email:<a href='spam@21cto.com'>spam@21cto.com</a>";
- echo "原始字符串=".$string."<br>";
- echo "目录字符串=".strip_tags($string)."<br>";
- ?>
- <?php
- $stringone="my story";
- $stringtwo="my story";
- if (strcmp($stringone, $stringtwo)==0){
- echo "两个字符串相同。<br>";
- }
- if (strncmp("php developer", "php", 3)==0){
- echo "两个字符前3个字符是相同的!";
- }
- ?>
- <?php
- $info="员工A:staff@21cto.com|思技创想,技术开发部";
- $tokens=":|,"; //$info字符串用了:|和全角逗号3种分隔符,以此标志分析
- $tokenized=strtok($info, $tokens);
- while ($tokenized){
- echo "值=$tokenized<br>"; //从$tokenized数组循环显示每
- 个元素
- $tokenized=strtok($tokens);
- }
- ?>
- <?php
- $text='Hello,Welcome to PHP5 world';
- echo strpbrk($text, 'W');
- echo "<br>";
- echo strpbrk($text, 'w');
- ?>