[size=small] 经过了[url=http://ryan-d.iteye.com/blog/1543225]《PHP开发笔记系列(一)-PDO使用》[/url],今天开了关于PHP开发中字符串的处理,[url=http://ryan-d.iteye.com/blog/1543244]《PHP开发笔记系列(二)-字符串使用》[/url],形成《PHP开发笔记系列》的第二篇。 字符串是任何开发语言都必须处理的,在PHP中字符串可以使用单引号(')或双引号(")进行定义。那单引号和双引号不同之处在哪?那就是双引号中的变量会被变量值替换,而单引号中的内容将原样输出。下面将日常程序开发中会碰到的字符串处理场景整理。[/size] [size=medium][b]1. 以数组形式访问字符串(strlen)[/b][/size] [code="php"] file:str-lengh.php url:http://localhost:88/str/str-lengh.php "; // for循环访问数组 //for($i=0; $i"; //} // while循环访问数组 $i=0; while($i"; $i++ } ?> [/code] [size=medium][b]2. 去除文本中的所有HTML标记(strip_tags)[/b][/size] [code="php"] file:str-strip-tags.php url:http://localhost:88/str/str-strip-tags.php hello world!
hello world!
hello world!
"; // 输出原始的字符串内容 echo "Original Text:"; echo $text."
"; // 去除所有html标签后进行输出 echo "Destination Text(After strip_tags)"."
"; echo strip_tags($text)."
"; // 字符串中的html标签不闭合 $text = "
hello world!"; // 去除所有html标签后进行输出 echo "Original Text:"; echo $text." "; // 去除所有html标签后进行输出 echo "Destination Text(After strip_tags)"." "; echo strip_tags($text)." "; ?> [/code] [size=small] 备注:如果$text的值是
hello world!,少了
,那么
将不会被strip_tags函数去除,从而影响后面的格式输出,使后续的所有输出都有h1标题的样式。[/size] [size=medium][b]3. 转义html实体(rawurlencode)[/b][/size] [code="php"] file:str-entities.php url:http://localhost:88/str/str-entities.php "; echo rawurlencode($text)." "; ?> [/code] [size=medium][b]4. 强制文本折行显示(wordwrap)[/b][/size] [size=small] wordwrap函数可以按照指定的字符串折行长度,将长文本进行折行。[/size] [code="php"] file:str-wordwrap.php url:http://localhost:88/str/str-wordwrap.php "; echo $text." "; echo $text.""; echo "Destination text(after wrap):"." "; echo wordwrap($text, 50, " ")." "; ?> [/code] [size=medium][b]5. 字符串定位与替换(strpos、str_replace)[/b][/size] [size=small] 字符串定位使用strpos函数,该函数返回一个字符串在另一个字符串出现的第一个位置,类似于JAVA中String类的indexOf()方法的作用:[/size] [code="php"] file:str-strpos.php url:http://localhost:88/str/str-strpos.php [/code] [size=small] 字符串替换使用str_replace函数,该函数替换部分字符串中的文本,类似于JAVA中String类的replace()方法的作用:[/size] [code="php"] file:str-strreplace.php url:http://localhost:88/str/str-strreplace.php "; echo $text." "; echo ""; echo "Destination text(replace):"." "; echo str_replace(" ", "__", $text)." "; ?> [/code] [size=medium][b]6. 字符串比较(substr_compare)[/b][/size] [size=small] 字符串比较可用于比较两个字符串间的大小,类似于JAVA中String的compare方法,如果返回值>0,代表第一个字符串比第二个大,反之第二个比第一个大,若为0,表示相等。[/size] [code="php"] file:str-compare.php url:http://localhost:88/file/str-compare.php [/code] [size=medium][b]7. 字符串截取(substr)[/b][/size] [size=small] 字符串截取可用于从字符串的指定位置截取指定长度的字串,用于子串值抽取很方便。[/size] [code="php"] file:str-sub.php url:http://localhost:88/file/str-sub.php '; echo 'Destination String: '.$newStr.' '; ?> [/code] [size=medium][b]8. 统计子串出现次数(substr_count)[/b][/size] [size=small] 统计子串在父串中出现的次数,可以使用substr_count函数。[/size] [code="php"] file:str-count.php url:http://localhost:88/file/str-count.php [/code] [size=medium][b]9. 字符串分拆与拼装(explode、implode)[/b][/size] [size=small] 字符串分拆可将一个字符串按照一个指定分隔符拆分成数组,类似于JAVA中String类的spilt()方法的作用。字符串组装时将字符串数组按照一个分隔符将数组中的数据进行拼装,形成一个新字符串。[/size] [code="php"] file:str-explode-implode.php url:http://localhost:88/str/str-explode-implode.php "; echo $text." "; echo ""; $sentenses = explode(". ", $text); echo "Destination text(explode):"." "; foreach ($sentenses as $sentense){ echo $sentense." "; } echo ""; $newText= implode($sentenses, ". "); echo "Destination text(implode):"." "; echo $newText." "; ?> [/code] [size=medium][b]10. 去除字符串的前后空格(trim)[/b][/size] [code="php"] file:str-trim.php url:http://localhost:88/str/str-trim.php "; echo strlen($text)." "; echo ""; echo "Destination text(trim):"." "; echo strlen(trim($text))." "; ?> [/code] [size=medium][b]11. 格式化输出(printf)[/b][/size] [size=small] 格式化输出使用printf函数或sprintf函数,类似于C语言中的printf函数的作用:[/size] [code="php"] file:str-printf.php url:http://localhost:88/str/str-printf.php [/code] [size=small][b] 本文地址:http://ryan-d.iteye.com/blog/1543225[/b][/size]