0

I know it has a lot of posts here to convert text link into clickable ones. But my situation is a little different.

I'd like to convert only links from my website. Not other websites.

So I tried:

$text = "The text you want to filter goes here. http://example.com/site/site";


echo preg_replace('$(\s|^)(https?://[example.com]+)(?![^<>]*>)$i', ' <a href="$2" target="_blank">$2</a> ', $text." ");

It works, but the problem is if the url contains something like: www.example.com/example/article/01/3232/ if will only get the example.com.

any ideas how to make it work as I want?

RGS
  • 3,189
  • 1
  • 20
  • 44
  • 1
    Currently you are using character classes wrongly. You only need `https?://example\.com(?:/[^\s/]+)*/?`. See live demo here https://regex101.com/r/tyN8jR/1 – revo Aug 08 '18 at 20:03
  • [Check this](https://tio.run/##RU/LCsIwELz7FUvpodWmET1ZFfEb1ItGpJTVFPoIyYr19e01CaKHnZ2dHWZZJVXfL1ZKqsEgJOwIlhBsJYLn9/YKt7whoBbOZUWo4dKiAYkaU5BEKuMcu7xWFaZFW3NTEnoI5jZP4@WE3U5XLpRHzv9yYF5ny2yZWGSCC37I2WPN9mM2E0ykx5FIv8rxOUmm78h6NsN4xW1qWOdUyGVgqXL5foz@pxLwb9jmN7GzlQ2ddPQT@v4D) for a working example, and this links can help you with this [link1](https://stackoverflow.com/questions/2762260/detecting-a-url-using-preg-match-without-http-in-the-string) and [link2](https://stackoverflow.com/questions/1492980/extracting-matches-from-php-regex) – Francisco Hahn Aug 08 '18 at 20:09

0 Answers0