1)字符串输出函数:
Echoprint():原样输出(区分单引号和双引号)
<?php
echo "Hello World"
?>
die()exit():输出字符串,结束脚本执行
printf():输出格式化字符串
<?php
$format = 'There are %d monkeys in the %s';
printf($format, $num, $location);
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n",$s);
?>
printf/sprintf %b %d %c %x %s %f %X
    b        整数转成二进位。
    c        整数转成对应的 ASCII 字符。
    trim函数phpd        整数转成十进位。
    f        单倍精确度数字转成浮点数。
    o        整数转成八进位。
    s        转成字符串。
    x        整数转成小写十六进位。
    X        整数转成大写十六进位。
sprintf():不直接输出格式化的字符串,返回格式化的字符串,保存到变量中
var_dump()打印变量的相关信息
2)字符串格式化函数
nl2br():PHP的换行 转换成HTML的换行标签,is_xhtml truefalse)来指定
<?php
echo nl2br("Welcome\r\nThis is my HTML document", false);
?>
wordwrap():在指定间隔的字符串后插入指定的字符串
<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
?>
strtolower($str),字符串转换为小写
strtoupper($str),字符串转换为大写
ucfirst($str),将字符串的第一个字符转换为大写
ucwords($str),将字符串中每个单词转换为大写
trim($str),去除字符串两端的空白字符。
ltrim($str),去除字符串左边空白字符。
rtrim($str),去除字符串右边空白字符。
空白字符:" ""\t""\n""\r"\0
str_pad():填充字符串到指定长度(用指定的字符串填充)
<?php
$input = "Alien";
echo str_pad($input, 10);                      //输出"Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // 输出"-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   //  输出"__Alien___"
echo str_pad($input, 6 , "___");               //  输出"Alien_"
?>
3)字符串长度
strlen($str),取得字符串长度
<?php
$str = 'abcdef';
echo strlen($str); // 6
$str = ' ab cd ';
echo strlen($str); // 7
?>
substr_count( ):统计包含的字符串个数
<?php
$text = 'This is a test';
echo strlen($text); // 字符串长度14
echo substr_count($text, 'is'); //字符串“is“搜索到 2个
echo substr_count($text, 'is', 3); //统计开始的位置是3,搜索有几个”is”
echo substr_count($text, 'is', 3, 3);// 统计开始的位置是3,搜索之后的3个字符有几个”is”
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');//统计连续的字符串,1个
?>
4)字符串的查和处理
string substr($string, $start[, $length]),返回字符串$string中由$start开始,长度为$length的子字符串
String strstr($string, $search),返回字符串$string中,$search第一次出现到字符串结束的子字符串。
<?php
  $email = 'USER@EXAMPLE';
  echo stristr($email, 'e');输出 ER@EXAMPLE
?>
int strpo( $str , $search [, int $offset ] ),查$search$str中第一次位的置,从$offset开始。
<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
Echo $pos;
?>
int strrpos( $str , $search [, int $offset ] ),查$search$str中最后一次的位置,从$offset开始
str_replace($search, $replace, $str),替换$str中的全部$search$replace
str_repeat(),重复输出指定的字符串
strcmp($str1,$str2),比较两个字符串,如果$str1大,则返回1$str2大,返回-1;相等,返回0
htmlspecialchars($str),函数把一些预定义的字符转换为HTML实体。
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
addslashes():在指定的预定义字符前添加反斜杠。这些字符是单引号(‘)、双引号(““)、反斜线( )NUL(NULL字符)
<?php
$str = "Who's John Adams?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";
?>
strip_tags():函数剥去 HTMLXML 以及 PHP 的标签
<?php
$text = '<p>Test paragraph.</p> Other text';
echo strip_tags($text);
echo "\n";
echo strip_tags($text, '<p>');
?>
    Number_format();格式化输出数字
    Md5();MD5加密字符串
    Strrev();字符串翻转
汉字的正则
//UTF8
//$str = "编程编程编程";
/
/if (preg_match("/([\x{4e00}-\x{9fa5}]){3,}/u",$str)) {
//print("该字符串全部是中文");
//} else {
//print("该字符串不全部是中文");
//GB2312
$str = "编程编程编程eee";
if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str)){   
    print("该字符串全部是中文");
}else{   
    print("该字符串不全部是中文");
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。