I found a working code online for auto-detecting url's from plain text. The problem I am having is creating an array to display multiple URL's if the user inputs multiple URL's into the plain text... I.E When they post a Post or Comment on my website, and lets say they have the following 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/.
With the code I have now... It will only display the first link and output the rest as plain text. I have searched for the past 7 days on-line but cannot find a working answer to this.
What I am looking for is how to turn the $url[0]
into an array that automatically increases the output value on the array by 1
(depending on the amount of links the user has inputted). So in this case it should display as $url[0]
then next link as $url[1]
and finally the third link as $url[2]
. But the main key is that I dont want a set (Fixed) number of arrays to be displayed. Instead
I am looking for an unlimited array output or a max of lets say 200 Limits. I say 200 because the use in which they will be used will
also be for a SEE ALSO type reference links.
My Code: (PHP)
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = $faq_seealso; // The variable $faq_seealso refers to my MySQL data fetch query.
// I will provide it if needed.
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "<a href={$url[0]}>{$url[0]}</a> ", $text); // <--- Part in question
} else {
// if no urls in the text just return the text
echo $text;
}
?>
Any help would be appreciated.
This is what the Output gives me with this code: (Snippet from my website)
1. https://www.pokemongosrf.ca https://www.pokemongosrf.ca
1. https://www.pokemongosrf.ca http://kambel.ca
As you can see from the Second Line, that the text I had input as 2 separate links are being displayed as 2 links with them both being identical to the first link that was placed in the Textfield. I am looking for them to be displayed as their own unique links and not a clone of the first link. Any help here would be greatly appreciated. Thank you.