您的位置:首页 > 编程语言 > PHP开发

php字符串的常用操作

2013-08-13 16:53 176 查看
$str = 'You\'re important to me and my job is to make you satisfied with our service and even to love it.';
echo '<p>'.strlen($str).'</p>'; # 计算字符串长度
echo substr_replace($str, '......', 10); # 将10位之后的内容用省略号代替
echo '<p>'.str_word_count($str).'</p>'; # 计算单词的个数

var_dump(explode(' ', $str)); # 将字符串转变为数组

$array = array(
'0' => 'hello',
'1' => 'world',
);
var_dump(implode(',', $array)); # 将数组转变为字符串
echo '<br />';

# 将文件中的网址用超链接标注
$url = "Google (http://www.google.com)";
$preg = "'(http|https)://([0-9a-zA-Z.]+)'si"; # 这里因为正则表达式中存在/,所以用'替代/,si代表的是匹配模式
$replacement = '<a href=\'$1://$2\'>$1://$2</a>';
$newUrl = preg_replace($preg, $replacement, $url);
print_r($newUrl);

# 去除字符串中的html标签
$httpStr = '<p>This is a demo</p>';
echo strip_tags($httpStr);

$str1 = 'asdfghj';
$str2 = 'asdvbng';
var_dump( strcmp($str1, $str2)); # 比较两个字符串是否一致
echo '<br />';
similar_text($str1, $str2); # 计算两个字符串的匹配度,使用第三个参数可以计算百分比

nl2br($str); # 将换行符转为html标签
wordwrap($str, 20); # 自动换行

本文出自 “贪狼大叔的咖啡馆” 博客,请务必保留此出处http://liuke86.blog.51cto.com/2211304/1272337
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: