使用PHP将regex替换为regex [英] Using PHP replace regex with regex

查看:134
本文介绍了使用PHP将regex替换为regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用相同的井号标签替换字符串中的井号标签,但是在添加了链接之后

I want to replace hash tags in a string with the same hash tag, but after adding a link to it

示例:

$text = "any word here related to #English must #be replaced."

我想将每个主题标签替换为

I want to replace each hashtag with

#English ---> <a href="bla bla">#English</a>
#be ---> <a href="bla bla">#be</a>

所以输出应该是这样的:

So the output should be like that:

$text = "any word here related to <a href="bla bla">#English</a> must <a href="bla bla">#be</a> replaced."

推荐答案

$input_lines="any word here related to #English must #be replaced.";
preg_replace("/(#\w+)/", "<a href='bla bla'>$1</a>", $input_lines);

演示

输出:

any word here related to <a href='bla bla'>#English</a> must <a href='bla bla'>#be</a> replaced.

这篇关于使用PHP将regex替换为regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆