Jump to content

Recommended Posts

This code gets what is between <title></title> in websites, so yes it returns exact title.

 

function getMetaTitle($content){
  $pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
  if(preg_match($pattern, $content, $match))
  return $match[1];
  else
  return false;
}

$url = 'http://127.0.0.1/';
$content = file_get_contents($url);
$title = getMetaTitle($content);

 

 

What i don't understand is how this line is being made:

 

$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";

 

It almost look like a "code" for me. Please explain.

Link to comment
https://forums.phpfreaks.com/topic/156409-understanding-patterns/
Share on other sites

The pattern <[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*> Means this:

 

<

Match <

 

[\s]*

Match a whitespace character 0 or more times

 

title

Match title

 

[\s]*

Match a whitespace character 0 or more times

 

>

Match >

 

([^<]+)

Match any character 1 or more times which is not < and store these in backreference 1

 

<

Match <

 

[\s]*

Match a whitespace character 0 or more times

 

/

Match /

 

[\s]*

Match a whitespace character 0 or more times

 

title

Match title

 

[\s]*

Match a whitespace character 0 or more times

 

>

Match >

Granted, there's no need to stuff \s within a character class (as in [\s]) if it is all by itself, as \s is already a shorthand character class for all whitespace characters.

 

Additional regex resources:

 

weblog tools collection

PHPFreaks Resources (tutorial links under 'Other Sources')

PHPFreaks regex tutorial

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.