I currently have the following code to add an a href to user-submitted plain text where HTTPS:// is found. The problem is that this obviously changes all links in the text to the same name/location. How can I do this process seperately for every instance of HTTPS:// in the text?
//Example variables (usually from MySQL)
$moreOrig = "https://duckduckgo.com is better than https://google.com";
// The Regular Expression filter
$testUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if (preg_match($testUrl, $moreOrig, $url)) {
//split into parts if user has a /something to clean url
$parts = explode ("/", $url[0]);
//glue
list($first, $second, $third) = $parts;
//output
$shortUrl = implode ("/", array($third));
$more = nl2br(preg_replace($testUrl, "<a href='" . $url[0] . "' rel = 'nofollow'>" . $shortUrl . "</a>", $moreOrig));
}
Output, desired vs actual ( assume input variable = "https://duckduckgo.com?q=Duck+Duck+Go is better than https://google.com?q=Duck+Duck+Go")
Desired:
<a href = "https://duckduckgo.com?q=Duck+Duck+Go">duckduckgo.com</a> is better than <a href = "https://google.com?q=Duck+Duck+Go">google.com.</a>
<br>
Actual:
<a href = "https://duckduckgo.com?q=Duck+Duck+Go">duckduckgo.com</a> is better than <a href = "https://google.com?q=Duck+Duck+Go">google.com.</a>
$1to put it back in where you use$url[0]now. However, to manipulate it to get a short url, you would probably needpreg_replace_callback().