-3

I want to get the content in the <title>...</title> of html label. I used the following code, but it doesn't work.

  <?php
$content = "<title>";
 preg_match_all("/<title>/",$content,$title);
echo $title[0][0];
 ?>

how to get the get the content in the <title>...</title> in php.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
runeveryday
  • 2,751
  • 4
  • 30
  • 44
  • 1
    You want to match *what* when your content only consists of ``...? – deceze Feb 10 '11 at 07:15
  • 8
    Could you please stop using your odd `Tag-Title-System`? That's what tags are for. – Bobby Feb 10 '11 at 07:17
  • 2
    the
    cannot hold it is too late. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
    – tobyodavies Mar 01 '11 at 22:40

1 Answers1

2

Are you trying to access the DOM, as you would in JavaScript? You can't do that in PHP unless you're reading an existing HTML page, or you're using output buffering on the page that PHP is generating.

In this case you could use:

<?php
$content = $the_entire_html_page_loaded_from_somewhere;
preg_match( '/<title>(.*)<\/title>/', $content, $title );
print_r( $title );
?>
Kalessin
  • 2,282
  • 2
  • 22
  • 24
  • thank you,in php i can use the file_get_contents to get the content.what's this preg_match( '/(.*)<\/title>/', $content, $title ); line meaning. – runeveryday Feb 10 '11 at 08:20
  • The regex groups the content in between the `` tags, so you can access it directly (if found) using `$title[1]`. – Kalessin Feb 10 '11 at 09:17