[PHP] Get URL Title


/ Published in: PHP
Save to your folder(s)

Return URL Title.
Return URL if any problem.


Copy this code and paste it in your HTML
  1. function get_url_title( $url ){
  2. try{
  3. $doc = new DOMDocument();
  4. @$doc->loadHTMLFile( $url );
  5. $xpath = new DOMXPath( $doc );
  6. $title = $xpath->query( '//title' );
  7. $title = $title->item( 0 );
  8. if( !( isset( $title->nodeValue ) ) ){
  9. return $url;
  10. }
  11. $title = $title->nodeValue;
  12. if( is_string( $title ) ){
  13. return $title;
  14. }
  15. return $url;
  16. }catch (Exception $e) {
  17. return $url;
  18. }
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.