#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    How to get the declared and used language in php


    Hi,

    I want to get the language defined in html or meta tags using php as <html lang="en"> using php. I have searched a lot in google but cannot find any way. Similarly i also want to get the language in site being used as if the site text is in japanese there shoould be any function to return the japense language using php. Is there any way to do this?? urgent help needed. Thanks
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,066
    Rep Power
    9398
    You can't look at the text and know for sure what language it's in. You can load the HTML into a DOMDocument and look for the <html lang> or anywhere else you want to check.

    Try using DOMDocument and post back when you have problems getting that to work.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by requinix
    You can't look at the text and know for sure what language it's in. You can load the HTML into a DOMDocument and look for the <html lang> or anywhere else you want to check.

    Try using DOMDocument and post back when you have problems getting that to work.
    <code>

    $html = file_get_contents($url);
    $doc = new DomDocument();
    $doc->loadHtml($html);
    foreach($dom->getElementsByTagName('html') as $link)
    {
    $this->onsite['language'] = $link->getAttribute('lang');
    return $link->getAttribute('lang');
    }

    </code>

    I have used this. But it is giving error oj loadHtml function. As i have googled it and is giving the results of some ampersand in html that is not converted into html text. Any idea on how to resolve it. Beside is this code correct for getting language attribute??
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,066
    Rep Power
    9398
    Are you sure it's giving you errors and not just warnings? If there's warnings about problems with the HTML itself there's probably not much you can do about it; throw in a call to libxml_use_internal_errors to suppress them.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by requinix
    Are you sure it's giving you errors and not just warnings? If there's warnings about problems with the HTML itself there's probably not much you can do about it; throw in a call to libxml_use_internal_errors to suppress them.
    yes its giving warnings but still i am unable to get the language. It si returning the empty results
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,066
    Rep Power
    9398
    If what you posted is really the code you're using then your lack of results could easily be explained by you using the wrong variable: should be $doc and not $dom. But you posted so little code that it could be any number of other problems at fault.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Pennsylvania, USA
    Posts
    35
    Rep Power
    2
    Are you sure that get_file_contents() is configured to be allowed to return URL contents?

    This is done with the "allow_url_fopen" php.ini setting.

    Otherwise, you can try using curl or fsockopen().

    Of course, if you have the raw HTML, you can easily parse for the language declaration yourself using some regular expressions.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by dynamicflash
    Are you sure that get_file_contents() is configured to be allowed to return URL contents?

    This is done with the "allow_url_fopen" php.ini setting.

    Otherwise, you can try using curl or fsockopen().

    Of course, if you have the raw HTML, you can easily parse for the language declaration yourself using some regular expressions.
    using curl its giving error DOMDocument::loadHTML() [<a href='domdocument.loadhtml'>domdocument.loadhtml</a>]: htmlParseEntityRef: expecting ';' in Entity, line: 48

IMN logo majestic logo threadwatch logo seochat tools logo