初学php——字符串

<?php
//字符串处理
//字符串长度
echo strlen("Hello");echo "<br>";
//取出子字符串
$string = "Nothing is impossible. Just do it!";
echo $just = substr($string,23,4);  echo "<br>";
echo $just = substr($string,-11,4);  echo "<br>";
echo $center = substr($string,11,11); echo "<br>";
echo substr($string,-11);    echo "<br>";
echo substr($string,-1);    echo "<br>";
//子字符串在母串中的位置;
//strpos()计算第一次出现的位置
//strrpos()计算最后一次出现的位置
$string = "wxyz wxyz";
echo $first = strpos($string,"x");echo "<br>";
echo $last  = strrpos($string,"x");echo "<br>";
echo $zero  = strpos($string,"w");echo "<br>";
echo $pos   = strrpos($string,"y");echo "<br>";
//访问字符串中的字符
$str= "There is some letters.";
echo $first = $str{0};echo "<br>";
echo $first = $str{3};echo "<br>";
echo $first = $str{strlen($str)-1};echo "<br>";
//字符串分割explode()
$this_year = 2009;
/*
$text = <<< EOT
祝无双,F,1982,广东,普通职员
李大嘴,M,1981,河北,普通职员
佟湘玉,F,1980,山西,项目经理
EOT;
*/
$text = "祝无双,F,1982,广东,普通职员\n李大嘴,M,1981,河北,普通职员\n佟湘玉,F,1980,山西,项目经理";
//print_r($text);echo "<br>";
$lines=explode("\n",$text);
foreach($lines as $userinfo){
 $info = explode(",",$userinfo);
 $name = $info[0];
 $sex  = ($info[1]=="F")?"女":"男";
 $age  = $this_year-$info[2];
 $city = $info[3];
 $zhiye = $info[4];
 echo "姓名:$name $sex 年龄:$age 城市:$city 职业:$zhiye<br>";
}
echo "<br>";
$str1 = "201,202,203,204,205";
$array = explode(",",$str1);
print_r($array);
echo "<hr>";
foreach($array as $values){
 echo $values;
 echo "<br>";
}
echo "<hr>";
//字符串连接implode() 或者 join()
echo "<br>";
$fruits = array('apple','banana','pear');
print_r($fruits);echo "<br>";
$imploded_string = join(",",$fruits);
echo $imploded_string;
echo "<br>";echo "<br>";
//删除字符串空白
$string = "   xxx There is something...";
echo $left=ltrim($string);echo "<br>";
echo $right=rtrim($string,".");echo "<br>";
echo $full=trim($string,"x .");echo "<br>";
//字符串反转
$str = "Hello World";
echo strrev($str);
echo "<br>";
//字符串大小写转换
$str1 = "Hello world!";
echo $lower = strtolower($str1);echo "<br>";//小写
echo $upper = strtoupper($str1);echo "<br>";//大写
$string = "hello PHP world!";
echo ucfirst($string);echo "<br>";   //此句首字母大写
$string = "hello PHP world!";
echo ucwords($string);echo "<br>";   //此句每个单词首字母大写

echo "<hr>";
//字符串比较,按照字节的ASCII码值比较,前者大于后者,返回一个负数;反之返回正数;如果相等,返回0值。
echo $test = strcmp("Thomas","Zom");echo "<br>";
if(strcmp("大连","大庆")>0){
 echo "大连";
}else{
 echo "大庆";
}
echo "<br>";
echo strcmp("Hello","Hello");echo "<br>";

echo strcasecmp("hello","HElLo");echo "<br>";//strcasecmp()忽略字符串大小写

//字符串替换

echo "<hr>";
$vowels = array("a","A","e","E","i","I","o","O","u","U");
$string = "Replace All Vowels in String.";
echo str_replace($vowels,"*",$string);
?>

你可能感兴趣的:(apple,PHP,F#)