views:

296

answers:

2

Hello all,

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!

+1  A: 

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
This is awesome! Works like a charm :) Great job Atli! Thank you
Kelvin
A: 

I have a quit similar problem. The code works perfect, However when I try to use it in my code it get NO MATCHES.

changing $post_title = "English textItalian text"; to $post_title = $post->post_title; results in no matches

and changing to $post_title = $post['post_title'];

Fatal error: Cannot use object of type stdClass as array in /wp-content/plugins/simple-facebook-connect/sfc-publish.php on line 199

Trying to echo gives this results:

$post_title = $post->post_title; echo $post_title; <!--:pt-->Projecto Pop’s<!--:--><!--:en-->POP’s project<!--:-->

Anyone a suggestion?? Kevin How did u fix qtranslate together with simple facebook connect?

Thanks for your time! Cheers Thijs

thijs