PHP 正则表达式 href 未设置标题
我正在尝试在 PHP 中创建一个函数,该函数将在字符串中搜索所有出现的 href,如果未设置标题,则应将其替换为 > 之间的文本值。文本
我不知道最好的方法是什么,考虑一下这样的事情:
$s = preg_replace('/< a[^>]*?href=[\'"](.*?)[\'"][^>]*?title=[\'"](.*?)[\'"][^>]*?>(.*?)<\/a>/si','< a href="$1" title="$2">$3</a>',$s);
我如何检查正则表达式以查看是否设置了 $2,如果没有将其替换为 $3,$3 也可以是类似 img 的东西src="..." alt="..." 在这种情况下我想获取 alt 的值。
首先,我想知道这是否可以在 PHP 中完成以及如何完成,但如果有任何帮助,我将不胜感激。
I'm trying to create a function in PHP that would search in a string for all a href occurences and if title is not set it should replace it with the text value between > text </a>
I don't know what is the best way to do it, thinking about something like:
$s = preg_replace('/< a[^>]*?href=[\'"](.*?)[\'"][^>]*?title=[\'"](.*?)[\'"][^>]*?>(.*?)<\/a>/si','< a href="$1" title="$2">$3</a>',$s);
How can I check in the regex to see if $2 is set and if it isn't replace it with $3, also $3 can be something like img src="..." alt="..." and in this case I would like to get the value of alt.
First of all I would like to know if this can be done in PHP and how, but any help would be apreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个无信息的链接在这里很合适。这对于正则表达式来说并不容易做到。例如,您不能使用带有前向反向引用的
(?!\4)
否定断言来将title=
与进行比较属性(这已经增加了提取的难度)。
至少您必须使用
preg_replace_callback
并在单独的函数中处理替换。在那里,可以更轻松地分解属性并将 alt= 与 title= 进行比较。如果您不使用它来重写输出,则可以通过不使用正则表达式来简化任务。从性能角度来看,这不是更好的选择,但可以使用例如 phpQuery 或 QueryPath 轻松完成:(
同样可以完成,只有更复杂的代码,使用 DOMDocument...)
The uninformative link is somehwat fitting here. That's not easily doable with regexpressions. You for example cannot use a
(?!\4)
negative assertion with forward backreference to compare thetitle=
against the<img alt=
attribute (which adds enough difficult for extraction already).At the very least you will have to use
preg_replace_callback
and handle the replacement in a separate function. There it's easier to break out the attributes and compare alt= against title=.If you aren't using this for output rewriting, then make the task simpler by not using regexpressions. This is performance-wise not the better choice, but easy to do with e.g. phpQuery or QueryPath:
(The same can be done, only with more elaborate code, using DOMDocument...)
也许假设它不会被设置并仅查找
title=''
:输出:
祝你好运。
编辑
抱歉,不太确定您的意思:
您的示例中的
$3
不是链接文本吗?Maybe presume it is not going to be set and look for
title=''
only:Output:
Good luck.
EDIT
Sorry, not too sure what you mean by:
Isn't
$3
in your example the link text?