How to extract URLs from text in PHP?

Spread the love

Sometimes, we want to extract URLs from text in PHP.

In this article, we’ll look at how to extract URLs from text in PHP.

How to extract URLs from text in PHP?

To extract URLs from text in PHP, we can use the preg_match_all function.

For instance, we write

preg_match_all("/[a-z]+:\/\/\S+/", $string, $matches);

to call preg_match_all with a regex that match URLs to get the URLs in $string.

The match values are put into the $matches array.

Conclusion

To extract URLs from text in PHP, we can use the preg_match_all function.

Leave a Reply

Your email address will not be published.