替换< a>标记为< b>使用PHP标记 [英] Replacing <a> tag with <b> tag using PHP

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

问题描述

好的,我有一段代码,内容如下:

< a title =titlehref =http://example.com> Text< / a>

OK, I have a section of code with things like:
<a title="title" href="http://example.com">Text</a>

我需要重新格式化它们以便它们变成:

< b>文本< / b>

I need to reformat these somehow so that they become:
<b>Text</b>

至少有24个链接被更改,并且它们都有不同的标题和hrefs 。在此先感谢,奥斯汀。

There are at least 24 links being changed, and they all have different titles and hrefs. Thanks in advance, Austin.

推荐答案

虽然不是最优的,但您可以使用正则表达式执行此操作:

Although not optimal, you can do this with regular expressions:

$string = '<a title="title" href="http://example.com">Text</a>';

$string = preg_replace("/<a\s(.+?)>(.+?)<\/a>/is", "<b>$2</b>", $string);

echo($string);

基本上说,查找具有形式的字符串的一部分< a *> {TEXT}< / a> ,复制 {文本} ,并用<$替换整个匹配的字符串c $ c>< b> {TEXT}< / b> 。

This essentially says, look for a part of the string that has the form <a*>{TEXT}</a>, copy the {TEXT}, and replace that whole matched string with <b>{TEXT}</b>.

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

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