D5145c421cd25af6fa577c15219add90

Extract and return the bits between the <title> and </title> tags of an html page.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
function get_title($html_page)
{
  // split the page into 3 sections, with the <title> and </title> tags as delimiters.
  $split_page = preg_split("%</?title[^>]*>%",$html_page);

  if (sizeof($split_page) == 3)
    return $split_page[1];
  else
    return "No title";
}
?>

Refactorings

No refactoring yet !

73415a883aec7c0a7aada9c4cdb208b5

Christoffer

February 9, 2010, February 09, 2010 16:49, permalink

2 ratings. Login to rate!
1
2
3
4
5
6
<?php
function get_title($html_page)
{
  return preg_match('/<title>(.+?)</title>/i', $html_page, $match) ? $match[1] : 'No title';
}
?>

Your refactoring





Format Copy from initial code

or Cancel