0

Wondering how I would detect a URL in a string and return a link. Example

$text = 'Go to www.example.com example.com example.net http://example.com'; echo $text;

would return

Go to www.example.com example.com etc.

2
  • 2
    I would use the search either on this website or on google to locate the highest rated answer on stackoverflow that solves the issue for me. But that might just be personal preference. You can also continue to wonder and wait that users here start to provide hacky code and half-ready solutions just by pure coincidence because they have just spotted you asking for it. Commented May 4, 2013 at 12:25
  • 1
    possible duplicate of PHP if string contains URL isolate it Commented Aug 23, 2015 at 13:32

2 Answers 2

1

You could use RegEx, or you could explode the $text string on spaces and then check with substring each exploded word if they begin with http://, https:// or end in .com, .net, .org and wrap that string in a link

Sign up to request clarification or add additional context in comments.

Comments

0

This is how I would do it.

It will echo http://google.com from

www.website.com and example.com BUT http://google.com and now www.website.com

<?php $x = "www.website.com and example.com and even http://google.com and now www.website.com I could go on forever"; $start = strpos($x, 'http://'); for($i=$start; $i < strlen($x); $i++){ if(strpos($x, '.com') == true) { $str = strpos($x, '.com'); echo $x[$i]; if($x[$i] === ' '){ break; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.