替换< 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>
,复制 {文本}
,并用< 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>
.