I would like to use preg_match in PHP to parse the site lang out of a html document;
My preg_match:
$sitelang = preg_match('!<html lang="(.*?)">!i', $result, $matches) ? $matches[1] : 'Site Language not detected';
When I have a simple attribute without any class or ids. For example: Input:
<html lang="de">
Output:
de
But when I have a other html code like this: Input:
<html lang="en" class="desktop-view not-mobile-device text-size-normal anon">
Output:
en " class=" desktop - view not - mobile - device text - size - normal anon,
I need just the lang code(en, de, en-En, de-DE).
Thanks for your advice or code.
UPDATE**
Another example when lang attribute is not coming as first element.
<html data-n-head-ssr lang="en">
Output:
Site Language not detected