Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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的自动增量数组_Php - Fatal编程技术网

Php 在纯文本中检测到多个URL的自动增量数组

Php 在纯文本中检测到多个URL的自动增量数组,php,Php,我在网上找到了一个从纯文本自动检测url的工作代码。我遇到的问题是创建要显示的数组 多个URL如果用户在纯文本中输入多个URL。。。即当他们在我的网站上发布帖子或评论时,以及 假设他们有以下文本: I found some really good articles on such and such topic. Here are a few links to check out: http://www.example.com/hOSDHOUA and https://www.mywebsite

我在网上找到了一个从纯文本自动检测url的工作代码。我遇到的问题是创建要显示的数组 多个URL如果用户在纯文本中输入多个URL。。。即当他们在我的网站上发布帖子或评论时,以及 假设他们有以下文本:

I found some really good articles on such and such topic. Here are a few links to check out: 
http://www.example.com/hOSDHOUA and https://www.mywebsite.com/h/yIFeelLowIfImHigh and 
http://example-site.com/today-is-a-beautiful-day/.
用我现在的代码。。。它将只显示第一个链接并输出 其余部分为纯文本。我已经在网上搜索了7天,但找不到有效的答案

我要寻找的是如何将
$url[0]
转换为一个数组,该数组会自动将数组上的输出值增加1 (取决于用户输入的链接数量)。因此,在这种情况下,它应该显示为
$url[0]
,然后下一个链接显示为
$url[1]
最后是第三个链接
$url[2]
。但关键是我不希望显示一组(固定)数量的数组。相反 我正在寻找一个无限的数组输出或最大的让我们说200限制。我说200是因为它们的用途将 另请参见类型参考链接

我的代码:(PHP)


从第二行可以看到,我输入的文本作为两个单独的链接显示为两个链接,它们都与文本字段中的第一个链接相同。我期待他们被显示为自己独特的链接,而不是第一个链接的克隆。这里的任何帮助都将不胜感激。谢谢。

我现在知道问题了

使用preg_match_all匹配所有链接并循环它们。
在循环中,您执行一个简单的str_replace,将链接替换为html锚

$text ="I found some really good articles on such and such topic. Here are a few links to check out: http://www.example.com/hOSDHOUA and https://www.mywebsite.com/h/yIFeelLowIfImHigh and http://example-site.com/today-is-a-beautiful-day/. ";
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls     

// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $text, $url)) {
    foreach($url[0] as $link){
        $text = str_replace($link, "<a href={$link}>{$link}</a> ", $text); // <--- Part in question
    }
    echo $text;
} else {
    // if no urls in the text just return the text
    echo $text;
}
//I found some really good articles on such and such topic. Here are a few links to check out: <a href=http://www.example.com/hOSDHOUA>http://www.example.com/hOSDHOUA</a>  and <a href=https://www.mywebsite.com/h/yIFeelLowIfImHigh>https://www.mywebsite.com/h/yIFeelLowIfImHigh</a>  and <a href=http://example-site.com/today-is-a-beautiful-day/.>http://example-site.com/today-is-a-beautiful-day/.</a>  
$text=“我找到了一些关于某个主题的非常好的文章。这里有一些链接可以查看:http://www.example.com/hOSDHOUA 及https://www.mywebsite.com/h/yIFeelLowIfImHigh 及http://example-site.com/today-is-a-beautiful-day/. ";
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
//要筛选URL的文本
//检查文本中是否有url
if(preg_match_all($reg_exUrl,$text,$url)){
foreach($url[0]作为$link){

$text=str_replace($link,“,$text);//我现在看到问题了

使用preg_match_all匹配所有链接并循环它们。
在循环中,您执行一个简单的str_replace,将链接替换为html锚

$text ="I found some really good articles on such and such topic. Here are a few links to check out: http://www.example.com/hOSDHOUA and https://www.mywebsite.com/h/yIFeelLowIfImHigh and http://example-site.com/today-is-a-beautiful-day/. ";
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls     

// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $text, $url)) {
    foreach($url[0] as $link){
        $text = str_replace($link, "<a href={$link}>{$link}</a> ", $text); // <--- Part in question
    }
    echo $text;
} else {
    // if no urls in the text just return the text
    echo $text;
}
//I found some really good articles on such and such topic. Here are a few links to check out: <a href=http://www.example.com/hOSDHOUA>http://www.example.com/hOSDHOUA</a>  and <a href=https://www.mywebsite.com/h/yIFeelLowIfImHigh>https://www.mywebsite.com/h/yIFeelLowIfImHigh</a>  and <a href=http://example-site.com/today-is-a-beautiful-day/.>http://example-site.com/today-is-a-beautiful-day/.</a>  
$text=“我找到了一些关于某个主题的非常好的文章。这里有一些链接可以查看:http://www.example.com/hOSDHOUA 及https://www.mywebsite.com/h/yIFeelLowIfImHigh 及http://example-site.com/today-is-a-beautiful-day/. ";
$reg|u exUrl=“/(http | https | ftp | ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”;
//要筛选URL的文本
//检查文本中是否有url
if(preg_match_all($reg_exUrl,$text,$url)){
foreach($url[0]作为$link){

$text=str_replace($link,“,$text);//您应该在替换字符串中使用
$0
替换regexp的对应匹配项。使用
$url[0]
只返回
preg_match()
中的第一个匹配项

echo preg\u replace($reg\u exUrl,“,$text);

您应该在替换字符串中使用
$0
替换regexp的对应匹配项。使用
$url[0]
只返回
preg\u match()中的第一个匹配项

echo preg\u replace($reg\u exUrl,“,$text);

使用
preg\u match\u all()
获取多个匹配项,然后在循环中替换它们。或者使用
preg\u replace()
中的URL regexp,而不仅仅是在
preg\u match()中找到的URL
。你能用你试图匹配的示例URL更新这个问题吗?@Barmar你能给我举个例子说明如何使用我的代码做到这一点吗?请不要重复这个问题。在我看来,这正是你想要的。使用
preg\u match\u all()
获取多个匹配项,然后在循环中替换它们。或者使用
preg\u replace()
中的URL regexp,而不仅仅是在
preg\u match()中找到的URL
。你能用你想要匹配的示例URL更新这个问题吗?@Barmar你能给我举个例子说明如何使用我的代码做到这一点吗?请不要重复这个问题。在我看来,这正是你想要的。我想你应该在回答中添加这一点:我认为OP不理解答案的不同。我认为你应该这样做在你的答案中加上这个:我认为OP不理解答案的不同
$text ="I found some really good articles on such and such topic. Here are a few links to check out: http://www.example.com/hOSDHOUA and https://www.mywebsite.com/h/yIFeelLowIfImHigh and http://example-site.com/today-is-a-beautiful-day/. ";
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls     

// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $text, $url)) {
    foreach($url[0] as $link){
        $text = str_replace($link, "<a href={$link}>{$link}</a> ", $text); // <--- Part in question
    }
    echo $text;
} else {
    // if no urls in the text just return the text
    echo $text;
}
//I found some really good articles on such and such topic. Here are a few links to check out: <a href=http://www.example.com/hOSDHOUA>http://www.example.com/hOSDHOUA</a>  and <a href=https://www.mywebsite.com/h/yIFeelLowIfImHigh>https://www.mywebsite.com/h/yIFeelLowIfImHigh</a>  and <a href=http://example-site.com/today-is-a-beautiful-day/.>http://example-site.com/today-is-a-beautiful-day/.</a>  
echo preg_replace($reg_exUrl, "<a href=$0>$0</a> ", $text);