代码之家  ›  专栏  ›  技术社区  ›  Kelvin

从Wordpress title-qtranslate插件返回文本的简单正则表达式

  •  3
  • Kelvin  · 技术社区  · 16 年前

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

    php代码是什么;是否使用正则表达式从该字符串返回文本和语言?

    谢谢!

    2 回复  |  直到 14 年前
        1
  •  7
  •   Atli    16 年前

    尝试以下方法:

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

    Array
    (
        [en] => English text
        [it] => Italian text
    )
    
        2
  •  1
  •   tchrist    14 年前

    这些都是很好的例子。但是,我最近发现qTranslate有自己的功能:

    qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($post_title);
    

    推荐文章