3

I am using qtranslate wordpress plugin to store blog content in multiple languages. Now I need to extract content from qtranslate tags.

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

What would be the php code & regular expression to return text and language from this string?

Thanks a lot!

Mazatec
  • 11,481
  • 23
  • 72
  • 108
Kelvin
  • 8,813
  • 11
  • 38
  • 36

2 Answers2

7

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";
}
?>

Prints:

Array
(
    [en] => English text
    [it] => Italian text
)
Atli
  • 7,855
  • 2
  • 30
  • 43
1

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

qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($post_title);

Which will grab the current language and fail over to the default.

tchrist
  • 78,834
  • 30
  • 123
  • 180
jphase
  • 11
  • 1