使用 PHP 用 <b> 标签替换 <a> 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5202496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:36:51  来源:igfitidea点击:

Replacing <a> tag with <b> tag using PHP

phphtml

提问by Austin Peterson

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

好的,我有一段代码,其中包含以下内容:
<a title="title" href="http://example.com">Text</a>

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

我需要以某种方式重新格式化这些,以便它们变成:
<b>Text</b>

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

至少有 24 个链接被更改,它们都有不同的标题和 href。提前致谢,奥斯汀。

回答by shmeeps

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></b>", $string);

echo($string);

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>.

这实质上是说,查找具有形式的字符串的一部分<a*>{TEXT}</a>,复制{TEXT},然后用 替换整个匹配的字符串<b>{TEXT}</b>

回答by Odyss3us

Try this,

尝试这个,

$link = '<a title="title" href="http://example.com">Text</a>';
echo $formatted = "<b>".strip_tags($link)."</b>";

Check thislink out as well, I think this is what you are looking for.

也请查看链接,我认为这就是您要查找的内容。

回答by Simon A. Eugster

You want to read about Regular Expressionsbecause you will need them sooner or later anyway. If you do not mind about the content of the href property, then you can use:

您想阅读正则表达式,因为无论如何您迟早都会需要它们。如果您不介意 href 属性的内容,则可以使用:

s/<a(?:\s[^>]*)?>([^<]+)<\/a>/<b><\/b>/

The part between the first // searches for the opening tag (either <a> alone or with some parameters, in this case a white space \s is required to avoid matching <abbrev> e.g. as well), some text which will stored by the brackets, and the closing tag. The part between the second // is the replacement part where \1 denotes the text matched by the brackets in the first part.

第一个 // 搜索开始标签之间的部分(单独的 <a> 或带有一些参数,在这种情况下需要一个空格 \s 以避免匹配 <abbrev> 例如),一些文本将由括号和结束标记。第二个 // 之间的部分是替换部分,其中 \1 表示第一部分中括号匹配的文本。

See also PHP's preg_replacefunction. The final expression would then look like this (tested):

另请参阅 PHP 的preg_replace函数。最终的表达式看起来像这样(经过测试):

preg_replace('/<a(?:\s[^>]*)?>([^<]+)<\/a>/i', '<b>\1</b>', '<a href="blabla">Text</a>');

回答by Yoann

To replace a tag by another and matching / replacing attributes names at the same time :

用另一个标签替换一个标签并同时匹配/替换属性名称:

$string = '<img src="https://example.com/img.png" alt="Alternative"/>';
$string = preg_replace('/<img src="(.+?)" alt="(.+?)"\/>/is','<amp-img src="" alt=""></amp-img>',$string);
//$string is now '<amp-img src="https://example.com/img.png" alt="Alternative"></amp-img>'