PHP字串比较

strcmp 比较两个字串的自然排序。相等为0,小于负数,大于为正数。strcasecmp 不区分大小写,strnatcmp按自然排序比较。

[cce_php]

<?php
$test1 = “123”;
$test2 = “1212”;
echo strcmp($test1,$test2).”<br />”; //显示1

echo strcasecmp($test1,$test2).”<br />”;//显示2

echo strnatcmp($test1,$test2).”<br />”;//显示-1
?>

[/cce_php]

PHP字符串分割函数

explode 字串分割返回数组:

[cce_php]

<?php
$email = “thqe@qq.com”;
$email_test = explode(‘@’,$email);
echo $email_test[0].”</br>”;
echo $email_test[1].”</br>”;
?>

[/cce_php]

implode或join 字串合并,为explode反函数:

[cce_php]

<?php
$email = “thqe@qq.com”;
$email_test = explode(‘@’,$email);
echo $email_test[0].”</br>”;
echo $email_test[1].”</br>”;

$email_add = implode(‘@’,$email_test);
echo $email_add;

$email_add = join(‘@’,$email_test);
echo $email_add;
?>

[/cce_php]

strtok字符串分割返回第一次分隔符前面的值:

[cce_php]

<?php
$test = ” My name is xiaomin”;
$test_str = strtok($test,”x”);
echo $test_str;
?>

[/cce_php]

substr 截取字串中的一段可以定义前后位置

[cce_php]

<?php
$test = ” My name is xiaomin”;
$test_str = substr($test,1,2);
echo $test_str;

$test_str = substr($test,1,-2);
echo $test_str;
?>

[/cce_php]

 

PHP输出函数

一般输出echo,格式输出printf

b:整数转二进制;

c:整数转字符;

d:整数转小数;

f:双精度转浮点数;

o:整数转八进制;

s:字符串转字符串;

u:整数转非指定整数;

x:整数转带小写字母的十六进制数

X:整数转带大写字母的十六进制数

[cce_php]

<?php
$test_a = 12;
$test_b = “xiaomin”;

//直接输出
echo “1name:$test_b”.”<br />”.”number:$test_a<br />”;

//字串格式化输出
printf(“2name:%s<br />number:%s<br />”,$test_b,$test_a);

//字串定义先后顺序(%位置\$)
printf(“3name:%2\$s<br />number:%1\$s<br />”,$test_a,$test_b);

//number三位小数输出 f代表浮点数
printf(“4name:%s<br />number:%.3f<br />”,$test_b,$test_a);

//显示%需要两个%%
printf(“5name:%s<br />number:%s%%<br />”,$test_b,$test_a);

?>

[/cce_php]

字符串格式化函数

清除多余字符:

trim清除开始和结束,ltrim清除开始,rtrim清除结束。

trim 函数去除字串开始和结束的空格,换行符或回车符(\n或\r),水平和垂直制表符(\t和\x0b)和字串结速符(\0),设定第二个参数会过滤自定义字符。

[cce_php]

<?php
$test = ” my name is “;
echo $test.”aa<br />”;
$test_a = trim($test);

echo $test_a.”aa<br />”;

?>

[/cce_php]

 

HTML格式化函数:nl2br 将字串中的换行符(\n)替换为<br />

[cce_php]

<?php
$test = ”
Chinadaily.com.cn is the largest English porta
l in China, providing news, business information,
BBS, learning materials. The Website has channels
as China.”;
echo nl2br($test);

?>

[/cce_php]

改变字母大小写:

[cce_php]

<?php
$test_a = “My name is Xiaomin “;
$test_b = “my name is xiaomin”;
//将字符串转为大写
echo strtoupper($test_a).”<br />”;

//将字串转为小写
echo strtolower($test_a).”<br />”;

//如果第一个是字母就将首字母转为大写
echo ucfirst($test_b).”<br />”;

//字符串每一个单词首字母大写
echo ucwords($test_b).”<br />”;
?>

[/cce_php]

为存入数库库需要的格式化:

引号,单引和双引,反斜杠\,NULL字符在数据库中会被当成控制符

addslashes函数会自动在以上字符前加转义符\

stripslashes函数会自动把以上字符前的转义符去掉

 

PHP邮件函数mail使用案例

[cce_php]

<?php
//create short variable names
$name=$_post[‘name’];
$email=$_post[’email’];
$feedback=$_post[‘feedback’];

//set up some static information

$toaddress = “thqe@qq.com”;
$subject = “Feedback from web site”;
$mailcontent=”Customer name:”.$name.”\n”.”Customer email:”.$email.”\n”.”Customer comments:\n”.$feedback.”\n”;

$fromaddress = “From: thqe@qq.com”;

//invoke mail() function to send mail
mail($toaddress,$subject,$mailcontent,$fromaddress);
?>
<html>
<head>
<title>Bob’s Auto Parts – Feedback Submitted</title>
</head>
<body>
<h1>Feedback submitted</h1>
<p>Your feedback has been sent.</p>
</body>
</html>

[/cce_php]

统计数组中元素个数

统计数组总元素个数count或sizeof

[cce_php]

<?php
$array = array( “a”,”b”,”c”,1,2,3);
echo count($array);

echo “<br />”;

echo sizeof($array);
?>

[/cce_php]

统计某一个数组元素在数组中出现的次数:

array_count_values

[cce_php]

<?php
$array = array( “a”,”b”,”c”,1,2,3,3,”a”,”a”);
$array_num = array_count_values($array);
while (list($key,$value) = each($array_num)){

echo “$key => $value <br />”;
}

[/cce_php]

 

对数组中任何元素操作函数

array_walk对数组中每一个值操作:

[cce_php]

<?php
$array = array( “a”,”b”,”c”,1,2,3);

function print_array($value){
echo “$value<br />”;
}
array_walk($array,’print_array’);
$myarray = array(“name”=>”zhangsan”,”number”=>”12″,”class”=>”one”);

function print_myarray($value,$key){
echo “$key=>$value<br />”;
}

//关联数组先为值,后为关键字
array_walk($myarray,’print_myarray’);
?>

[/cce_php]

 

PHP数组指针操作

each() 数组当前指针元素;

current() 数组第一个指针元素;

next() 数组下一个指针元素;

prev() 数组上一个指针元素;

reset() 返回指针到第一个元素;

end() 将指针移动数组结尾

 

PHP提取文本文件到数组

[cce_php]

<?php
//Create short variable name
$DOCUMENT_ROOT = $_SERVER[‘DOCUMENT_ROOT’];
?>
<html>
<head>
<title>
Bob’s Auto Parts – Customer Orders
</title>
</head>

<body>
<h1>Bob’s auto Parts</h1>
<h2>Customer Orders</h2>
<?php
//Read in the entire file.
//Each order becomes an element in the array

$orders= file(“$DOCUMENT_ROOT/../2015/orders.txt”);
//读取所有文件到$orders函数
$number_of_orders = count($orders);
//定义总行数
if ($number_of_orders == 0) {
echo “<p><strong>No orders pending. Please try again later.</strong></p>”;
}
echo “<table border=\”1\”>”;
echo “<tr><th bgcolor=\”#CCCCFF\”>Order Date</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Tires</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Oil</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Spark</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Total</th>”;
echo “<th bgcolor=\”#CCCCFF\”>Address</th></tr>”;

for ($i=0; $i<$number_of_orders; $i++) {
$line = explode(“\t”, $orders[$i]);

//将字符分割成以’\t’为分隔的小块

$line[1]= intval($line[1]);
$line[2]= intval($line[2]);
$line[3]= intval($line[3]);

//将字符转换为整数

echo “<tr>

<td>”.$line[0].”</td>
<td align=\”right\”>”.$line[1].”</td>
<td align=\”right\”>”.$line[2].”</td>
<td align=\”right\”>”.$line[3].”</td>
<td align=\”right\”>”.$line[4].”</td>
<td>”.$line[5].”</td>
</tr>”;
}

echo “</table>”;
?>

</body>
</html>

[/cce_php]