Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to extract URL from string in PHP

2 Answers

0 votes
$s = "website: https://www.collectivesolver.com/ Programming & Software Q&A";

$regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
 
preg_match($regex, $s, $url);
 
print_r($url);

    

/*
run:

Array
(
    [0] => https://www.collectivesolver.com/
    [1] => https
    [2] => /
)

*/

 





answered Aug 14, 2020 by avibootz
edited Aug 14, 2020 by avibootz
0 votes
$s = "website: https://www.collectivesolver.com/ Programming & Software Q&A";

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $s, $url);

print_r($url);

    

/*
run:

Array
(
    [0] => Array
        (
            [0] => https://www.collectivesolver.com/
        )

    [1] => Array
        (
            [0] => /
        )

)

*/

 





answered Aug 14, 2020 by avibootz

Related questions

2 answers 111 views
1 answer 62 views
1 answer 87 views
1 answer 164 views
1 answer 85 views
...