PHP: Extracting Specified String

This example, we will see how to extract the particular string mentioned in the preg_match first parameter from the selected string https://rafeekphp.wordpress.com/index.html.

The below is the code for the same,

preg_match(“/^(http:\/\/)?([^\/]+)/i”,”https://rafeekphp.wordpress.com/index.html“, $matches);
$host = $matches[2];
echo $host;

 Output is :   
                  rafeekphp.wordpress.com

The second example illustrate the string extracting  “wordpress.com” from the string  $host, the code and output will be as below,

preg_match(“/[^\.\/]+\.[^\.\/]+$/”,$host,$matches);
echo “domain name is: “.$matches[0].”\n”;

Output is:
                            wordpress.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: