Simple regex to return text from Wordpress header - qtranslate plugin

I use the qtranslate wordpress plugin to store blog content in several languages. Now I need to extract the content from the qtranslate tags.

$post_title  = "<!--:en-->English text<!--:--><!--:it-->Italian text<!--:-->";

What will be the PHP code and regular expression for returning text and language from this line?

Thanks a lot!

+3
source share
2 answers

Try something like:

<?php
$post_title  = "<!--:en-->English text<!--:--><!--:it-->Italian text<!--:-->";

$regexp = '/<\!--:(\w+?)-->([^<]+?)<\!--:-->/i';
if(preg_match_all($regexp, $post_title, $matches))
{
    $titles = array();
    $count = count($matches[0]);
    for($i = 0; $i < $count; $i++)
    {
        $titles[$matches[1][$i]] = $matches[2][$i];
    }
    print_r($titles);
}
else
{
    echo "No matches";
}
?>

Print

Array
(
    [en] => English text
    [it] => Italian text
)
+6
source

These are all brilliant examples. However, I recently discovered that qTranslate has its own function:

qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($post_title);

Which will capture the current language and go to default.

+1
source

Source: https://habr.com/ru/post/1724834/


All Articles