0

我正在尝试在方括号中的标签之间获取文本。

让我们看看简单的字符串示例:

[法语]这是法语文本[/french][english]这是英语文本[/english]

然后在 PHP 中(使用https://stackoverflow.com/a/9826656/962734):

private function translate($string)
{
    $start = "[".$this->lang."]";
    $end = "[/".$this->lang."]";
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return $string;
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

更复杂的解决方案怎么样?

我的意思是,如果未设置语言-> 使用默认值(英语)。如果字符串包含标签外的字符串,则沿所选语言显示。如果字符串不包含请求的语言 -> 显示默认值(或者如果未设置默认值,则显示首先在字符串中找到。示例:

$this->language->__set("defLang", "english");

$str = "This is a [french]French text[/french][english]English text[/english]";
$this->language->lang = "french"; 
return $this->language->translate($str);
//OUTPUT: This is a French text

$str = "This is a [deutch]Deutch text[/deutch][english]English text[/english]";
$this->language->lang = "french"; 
return $this->language->translate($str);
//OUTPUT: This is a English text

$str = "This is a [french]French text[/french][deutch]Deutch text[/deutch]";
$this->language->lang = "english"; 
return $this->language->translate($str);
//OUTPUT: This is a French text

使用 PHP 执行此操作的最佳方法是什么?我怀疑它需要复杂的正则表达式,但由于我不熟悉正则表达式,我想知道是否还有其他方法。

编辑:

根据 jeff ( https://stackoverflow.com/a/17727494/962734 ) 提供的答案,我制作了在大多数情况下都可以使用的功能。谢谢。

function translate($text)
{
    $exp = '/(\[(.+?)\])(.+?)\[\/.+?\]/i';
    $m = preg_match_all( $exp, $text, $matches);

    if($m){

        $mtchs = $matches[0];
        $langs = $matches[2];
        $texts = $matches[3];
        $c = 0;
        $foundLang = false;
        $foundFirstOptionalLang = false;

        foreach($langs as $l){
            if($l == $this->lang){
                $text = str_replace($mtchs[$c], $texts[$c], $text);
                $foundLang = true;
            }else{
                if(!$foundFirstOptionalLang && $l == $this->defLang){
                    $optionalText = str_replace($mtchs[$c], $texts[$c], $text);
                    $foundFirstOptionalLang = true;
                }
                $text = str_replace($mtchs[$c], "", $text);
            }
            $c++;
        }
        if (!$foundLang) $text = $optionalText;
    }

    return $text;
}
4

3 回答 3

1

你可以使用类似的东西:

$exp = '/(\[(.+?)\])(.+?)\[\/.+?\]/i';
$str = "This is a [french]French text[/french] and [english]English text[/english]";

$m = preg_match_all( $exp, $str, $matches);
echo "<pre>";
var_dump( $matches);
echo "</pre>";

这将返回

array(4) {
  [0]=>
  array(2) {
    [0]=>string(28) "[french]French text[/french]"
    [1]=>string(31) "[english]English text[/english]"
  }
  [1]=>
  array(2) {
    [0]=> string(8) "[french]"
    [1]=> string(9) "[english]"
  }
  [2]=>
  array(2) {
    [0]=>string(6) "french"
    [1]=>string(7) "english"
  }
  [3]=>
  array(2) {
    [0]=>string(11) "French text"
    [1]=>string(12) "English text"
  }
}

因此,您将访问数组 [2] 获取语言名称,访问数组 [3] 获取实际文本。

于 2013-07-18T15:18:12.157 回答
1

对于这种特殊情况:

function getTextInTag($text,$tag='english')
{
    $innerText = preg_replace('#^.*\['.$tag.'\](.+?)\[/'.$tag.'\].*$#','$1',$text);
    if($innerText == $text) $innerText = preg_replace('#^.*\[english\](.+?)\[/english\].*$#','$1',$text);
    $text = preg_replace('#(\[[^\]]+\].+?\[/[^\]]+\])+#','',$text);
    return $text.$innerText;
}

getTextInTag('This is a [french]French text[/french][english]English text[/english]'); //This is a English text
getTextInTag('This is a [french]French text[/french][english]English text[/english]','french'); //This is a French text
getTextInTag('This is a [french]French text[/french][english]English text[/english]','spanish'); //This is a English text
于 2013-07-18T15:22:06.147 回答
0

尝试这个:

 function translate($str){
      // If the language has been set use it, otherwise use the default value         
      $lang = isset($this->language->lang)?$this->language->lang:$this->language->defLang;

      if(preg_match("/\[$lang\](.+)\[\/$lang\]/", $str, $m)){
           return $m[1];              
      }else{
           // There was no text found for $lang,  fall back to english         
           preg_match("/\[english\](.+)\[\/english\]/", $str, $m);
           return $m[1];
      }


 }
于 2013-07-18T15:16:49.577 回答