1

I am getting these random synopsis details as a string, but the odd one or two contain links to URL's etc. These are raw strings and contain no html tags, just pure strings.

So the urls within the strings would simply be http://www...ect etc or even just www.name... etc.

What I'm trying to do is to convert these into clickable links, so when they output to the browser they actually are links rather than a string url that a user would have to copy and paste etc.

An example of the string would be:

Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi possimus nisi a quis, voluptas adipisci asperiores, earum aut totam sequi necessitatibusrepellat, quasi labore molestiae. Laboriosam quis vitae unde http://www.websitename.com?title=oy96 natus.earum aut totam sequi necessitatibus repellat, quasi labore molestiae. Laboriosam quis vitae unde natusearum aut totam sequi necessitatibus repellat, quasi labore molestiae. Laboriosam quis vitae unde natus

Would be good to turn http://www.websitename.com?title=oy96 into a clickable link with just the domain name.www.websitename.com

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
  • Do you mean converting http://www.websitename.com?title=oy96 to http://www.websitename.com ?? – Ravinder Reddy Oct 03 '17 at 14:13
  • Maybe https://regex101.com/r/1LgEps/2/ or if the protocol also should be removed move that to its own capture group as well. – chris85 Oct 03 '17 at 14:29
  • Though you might have your reasons, it might be bad for userinterface. What if the same domain is refered to a few times, with different articles? – Martijn Oct 03 '17 at 14:35
  • 1
    Possible duplicate of [Extract URLs from text in PHP](https://stackoverflow.com/questions/910912/extract-urls-from-text-in-php) – ctwheels Oct 03 '17 at 15:31
  • what the demo copy doesnt show there is a flat copy of the link, SO has added the link. Bascially grab the string URL convert to a URL firstly and the easiest, or then also just using the main domain as display text. It wont have more than a few links if at all any, so does not matter if the same main URL? Thanks for the replies – Simon Davies Oct 03 '17 at 15:57
  • Linked regex does what you need or https://regex101.com/r/1LgEps/3/ does it? – chris85 Oct 03 '17 at 16:04

1 Answers1

0

Just have written the perfect solution to your problem:

function webAddressesToHTML($text,$label_strip_params=false,$label_strip_protocol=true) {

    $webAddressToHTML = function ($url) use ($label_strip_params,$label_strip_protocol) {
        $label = $url;
        if($label_strip_params) {
            $label = rtrim(preg_replace('/\?.*/', '',$label),"/");
        }
        if($label_strip_protocol) {
            $label = preg_replace('#^https?://#', '', $label);
        }
        return '<a href="'.((!preg_match("~^(?:f|ht)tps?://~i", $url)) ? "http://".$url : $url).'">'.$label.'</a>';
    };

    preg_match_all('@(http(s)?://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@',$text,$matched_urls);
    return str_replace($matched_urls[0],array_map($webAddressToHTML,$matched_urls[0]),$text);
}

See the example:

$demo = "After the latest show, we went to www.example.com and made some trouble online. https://www.letsfail.net/?a=b gave us their support and also netjunkies.sample is involved. Finally we are in!";

var_dump(webAddressesToHTML($demo));

Output:

string(302) "After the latest show, we went to <a href="http://www.example.com">www.example.com</a> and made some trouble online. <a href="https://www.letsfail.net/?a=b">www.letsfail.net/?a=b</a> gave us their support and also <a href="http://netjunkies.sample">netjunkies.sample</a> is involved. Finally we are in!"

Depending on how you want to have your web address label, just set the optional parameters to true or false. If you really want to strip get parameters depends on your use case.

Blackbam
  • 17,496
  • 26
  • 97
  • 150