0
    <?php


     $content=file_get_contents('example.com');

   // it would return html <head>..... 
  <title>Example.com</title>

I want to extract example.com from title

  $title=pick('<title>','</title>',$content);

   Echo $title;

And it would show Example.com

The Ghost
  • 11
  • 2
  • 1
    Hi, welcome to stackoverflow. Please read the [how to ask](https://stackoverflow.com/help/how-to-ask) section in order to provide a complete and answerable question. In your case, you should consider adding what you've tried so far. – Nicolas Jul 12 '19 at 02:56
  • https://www.php.net/manual/en/class.domdocument.php might help – brombeer Jul 12 '19 at 06:33

3 Answers3

0

You can use substr to substring the HTML content and stripos to find the title tags.
I add 7 to the position to remove the tag.

$html = file_get_contents('example.com');
$pos = stripos($html, "<title>")+7;
echo substr($html, $pos, stripos($html, "</title>")-$pos);

Example:
https://3v4l.org/qvC40

This assumes there is only one title tag on the page, if there is more then it will get the first title tag.

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

You can use file_get_content() instead of $string.

    $string = "<title>MY TITLE</title>";

    $pattern = "/<title>(.*?)<\/title>/";

    preg_match($pattern, $string, $matches);

    echo "RESULT : ".$matches[1];
0

Try using PHP's simple xml parser to read the title node.

$xml = simplexml_load_string(file_get_contents('example.com'));
echo $xml->head->title;
Bram Verstraten
  • 1,414
  • 11
  • 24