php正则字符串替换的实现方法
2022-12-02 09:01:16
114
{{single.collect_count}}

php正则字符串替换的实现方法:首先创建一个PHP示例文件;然后通过表达式“preg_match_all('/(http:\/\/blog.com).*?(php)/is',"aaaa...”实现字符串替换即可。

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

推荐:《PHP视频教程

本教程操作环境:windows7系统、PHP5.6版,该方法适用于所有品牌电脑。

php正则字符串替换的实现方法:

需求是将一段内容中的某个特定字符串后面添加 一些字符串

最好是用到正则替换

preg_match_all('/(http:\/\/blog.com).*?(php)/is',"aaaahttp://blog.com/sss/index.phpsdsdahttp://blog.com/dssssss/index.phpsdhttp://blog.com/dssssss/index.phpsd", $data);var_dump($data);die;
登录后复制

关键问题是正则表达式怎么写

这里的是 .*匹配任意字符串?(php)当碰到php的时候停止

/////到时补充下有关正则//
登录后复制

---------------------------------------多余的--------------------------------------

学习了几个字符串操作的函数

strpos 查找字符串位置

$newstring = 'abcdef abcdef';$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0第三个参数是标记从哪里开始找
登录后复制

有一个可以查找所有出现位置的方法

function getCharpos2($str, $char){ $j = 0; $arr = array(); $count = substr_count($str, $char); for($i = 0; $i < $count; $i++){ $j = strpos($str, $char, $j); $arr[] = $j; $j = $j+1; } return $arr;}
登录后复制
str_replace 这个没什么好说的 就是替换
登录后复制

该方法适用于所有品牌的电脑。

以上就是php正则字符串替换的实现方法的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}