ITPub博客

首页 > IT职业 > IT生活 > php字符串的常用操作

php字符串的常用操作

IT生活 作者:castlight 时间:2021-09-09 09:22:15 0 删除 编辑


$str = 'You're important to me and my job is to make you satisfied with our service and even to love it.';
echo '

'.strlen($str).'

';    # 计算字符串长度
echo substr_replace($str, '......', 10);    # 将10位之后的内容用省略号代替
echo '

'.str_word_count($str).'

';    # 计算单词的个数

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

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

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

# 去除字符串中的html标签
$httpStr = '

This is a demo

';
echo strip_tags($httpStr);


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

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1470/viewspace-2809737/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    102
  • 访问量
    1239