[英]Regex TYPO3 Links
I need to change all TYPO3 Links into real a-Tags, because I want to use it without TYPO3我需要将所有 TYPO3 链接更改为真正的 a-Tags,因为我想在没有 TYPO3 的情况下使用它
Text example #1:文本示例 #1:
$strText = 'This is a <strong>full</strong> Text
<link https://www.example.com/news/test.html#tab3 _blank external-link-new-window "Opens external link in new window">More informations</link><br>
Some more Text <link http://www.example.com>More informations</link>
Text end';
I tried the following regex:我尝试了以下正则表达式:
$search = '/<link\s(ftp:\/\/|http:\/\/|https:\/\/|\/)([^\s]+)[^>]*>(.+?)<\/link>/si';
$replace = '<a href="\\1\\2" rel="nofollow" target="_blank" class="external">\\3</a>';
$strText = preg_replace($search, $replace, $strText);
It is working perfectly with this text... However it is very buggy, if the order of the links is inverted inside this text它与此文本完美配合......但是它非常有问题,如果链接的顺序在此文本中颠倒
Text example #2 which result in problems:导致问题的文本示例#2:
$strText = 'This is a <strong>full</strong> Text
<link http://www.example.com>More informations</link><br>
Some more Text <link https://www.example.com/news/test.html#tab3 _blank external-link-new-window "Opens external link in new window">More informations</link>
Text end';
This results in a incorrect link which wraps both links in one.这会导致将两个链接合二为一的错误链接。
I don't understand this and can't find a solution.我不明白这一点,也找不到解决方案。
使用这个模式:
/<link\s(ftp:\/\/|https?:\/\/|\/)([^\s]+)[^>]*>(.+?)<\/link>/ig
Andy's answer was a very good hint, but that regex was not working for links that have no more attributes other than the href eg (<link https://docs.google.com/spreadsheets/d/1example/edit#gid=3333333> some text )安迪的回答是一个很好的提示,但该正则表达式不适用于除了 href 之外没有更多属性的链接,例如 (<link https://docs.google.com/spreadsheets/d/1example/edit#gid=3333333 >一些文字)
This worked fine:这工作得很好:
<link(\s(ftp:\/\/|https?:\/\/|\/)([^\s>]+))[^>]*>(.+?)<\/link>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.