php: Extract URLs from String

2019-08-07 2019-12-30 00:24:21 PHP,string,array,EN

Q: How to extract URLs from a string with php ?

A: use the php function preg_match_all to acvhieve this.


// example string with URLs
$sContent = ', FOO BAR BLA BLA, BLA BLA';

$sPattern = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
    $aMatch, // <= saves result into $aMatch

a var_dump($aMatch); will then give you

array(1) {
  array(3) {
    string(24) ""
    string(24) ""
    string(24) ""

This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".