Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么这个正则表达式只能在一个Url上工作,而不能在另一个Url上工作?_Php_Regex_Preg Replace_Bbcode - Fatal编程技术网

Php 为什么这个正则表达式只能在一个Url上工作,而不能在另一个Url上工作?

Php 为什么这个正则表达式只能在一个Url上工作,而不能在另一个Url上工作?,php,regex,preg-replace,bbcode,Php,Regex,Preg Replace,Bbcode,因此,我使用这个正则表达式将twitter上的所有帖子转换为嵌入式推文: ”~(?: 编辑: 以下是完整的正则表达式和替换: 在不知道看到一些您试图从中提取的示例URL的情况下,很难绝对肯定地说,但也许我可以提供一些一般性建议 ([^&]+)发布代码和您正在调用的字符串的内容preg_replace()。@Barmar我发布了它。您可以添加输入和预期输出吗?对于facebook URL,我得到了。它应该是什么呢?我确实发布了,应该是。当我更改所有这些内容时,它对我有效。 $search = ar

因此,我使用这个正则表达式将twitter上的所有帖子转换为嵌入式推文:

”~(?:

编辑:

以下是完整的正则表达式和替换:


在不知道看到一些您试图从中提取的示例URL的情况下,很难绝对肯定地说,但也许我可以提供一些一般性建议


([^&]+)
发布代码和您正在调用的字符串的内容
preg_replace()
。@Barmar我发布了它。您可以添加输入和预期输出吗?对于facebook URL,我得到了
。它应该是什么呢?我确实发布了,应该是
。当我更改所有这些内容时,它对我有效。
$search = array (
    '~\[b](.*?)\[/b]~is',
    '~\[i](.*?)\[/i]~is',
    '~\[u](.*?)\[/u]~is',
    '~\[ul](.*?)\[/ul]~is',
    '~\[li](.*?)\[/li]~is',
    '~\[user=(.*?)](.*?)\[/user]~i',
    '~\[url=https?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12})].*?\[/url]~i',
    '~\[url]https?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12}).*?\[/url]~i',
    '~\[url=((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)](.*?)\[/url]~i',
    '~\[url]((?:ht|f)tps?://[a-z\d.-]+\.[a-z]{2,3}/\S*?)\[/url]~i',
    '~\[img=(.*?)].*?\[/img]~i',
    '~\[quote](.*?)\[/quote]~is',
    '~\[code](.*?)\[/code]~is',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|(?:\bhttps?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12}))\S*~i',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|\bhttps?://(?:www:)?clips\.twitch\.tv/([^&]+)\S*~i',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|\bhttps?://(?:www:)?imgur\.com/gallery/([^&]+)\S*~i',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|\bhttps?://(?:www:)?twitter\.com/([^&]+)/status/([^&]+)\S*~i',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|\bhttps?://(?:www:)?facebook\.com/([^&]+)/posts/([^&]+)\S*~i',
    '~(?:<a.*?</a>|<img.*?</img>|<iframe.*?</iframe>)(*SKIP)(*FAIL)|\bhttps?://.+?(?=\s|$)~im'
);

$replace = array (
    '<strong>$1</strong>',
    '<em>$1</em>',
    '<u>$1</u>',
    '<ul>$1</ul>',
    '<li>$1</li>',
    '<a href="../login/profile?u=$1" target="_blank">$2</a>',
    '<br><iframe width="600" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><br>',
    '<br><iframe width="600" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><br>',
    '<a href="$1" target="_blank" rel="nofollow">$2</a>',
    '<a href="$1" target="_blank" rel="nofollow">$1</a>',
    '<img src="$1"></img>',
    '<quote>$1</quote>',
    '<code>$1</code>',
    '<br><iframe width="600" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe><br>',
    '<br><iframe width="600" height="315" src="//clips.twitch.tv/embed?clip=$1&autoplay=false" frameborder="0" allowfullscreen></iframe><br>',
    '<blockquote class="imgur-embed-pub" lang="en" data-id="$1"><a href="//www.imgur.com/$1"></a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>',
    '<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr"><a href="//twitter.com/$1/status/$2?ref_src=twsrc%5Etfw"></a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>',
    '<iframe src="//www.facebook.com/plugins/post.php?href=//www.facebook.com/$1/posts/$2&width=500" width="500" height="705" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allow="encrypted-media"></iframe>',
    '<a href="$0" target="_blank" rel="nofollow">$0</a>'
);