I have this text:
$string = "this is my friend's website http://example.com I think it is coll";
How can I extract the link into another variable?
I know it should be by using regular expression especially preg_match()
but I don't know how?
I have this text:
$string = "this is my friend's website http://example.com I think it is coll";
How can I extract the link into another variable?
I know it should be by using regular expression especially preg_match()
but I don't know how?
I tried to do as Nobu said, using Wordpress, but to much dependencies to other WordPress functions I instead opted to use Nobu's regular expression for
preg_match_all()
and turned it into a function, usingpreg_replace_callback()
; a function which now replaces all links in a text with clickable links. It uses anonymous functions so you'll need PHP 5.3 or you may rewrite the code to use an ordinary function instead.URLs have a quite complex definition — you must decide what you want to capture first. A simple example capturing anything starting with
http://
andhttps://
could be:Note that this is very basic and could capture invalid URLs. I would recommend catching up on POSIX and PHP regular expressions for more complex things.
The code that worked for me (especially if you have several links in your $string) is:
Hope that helps others as well.
This Regex works great for me and i have checked with all types of URL,
Checked with lots of URL's can find here http://www.rubular.com/r/bFHobduQ3n