Jump to content

Understanding patterns?


godsent

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.