如何拆分此行:
我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him
分为三行:
我 [wǒ] - (pronoun) I or me
你 [nǐ] - (pronoun) you (second person singular); yourself
他 [tā] - (pronoun) he or him
让我们说,在每一行之后插入<br />
标记?
谢谢!
UPD。我的坏,有时期,但这是一个错误。
答案 0 :(得分:2)
自从你删除点以来我们可以看到的唯一清晰图案是“外来字符,空格和开放括号”。
让我们专注于此:
<?php
$string = "我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him";
$result = preg_replace('/(. \[)/u', // "any char, a space then [", 'u' flag to use UTF8
'<br/>$1', // replace it by a break table and a back reference
$string);
echo $result;
请注意,使用此算法,换行符将位于行的开头。 不要忘记UTF-8标志,并在应用程序的任何地方使用UTF-8,否则处理字符串会很乱。
编辑:如果您希望换行符仅在两行的开头,那么您可以使用negative lookbehind来实现此目的:
$string = "我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him";
// the same pattern, but excluding the one preceded by "^", where the string starts
$result = preg_replace('/(?<!^)(. \[)/u',
'<br/>$1',
$string);
echo $result;
答案 1 :(得分:0)
如果您对格式有所了解,可以尝试这样的方法,但如果没有合适的分隔符,那么只是猜测而且您可能会得到错误的转换。
$str = preg_replace("/\s+(\S+\s+\[\S+\])/", "<br />$1", $str);
答案 2 :(得分:0)
如果我的解释是正确的,你想在每个中国/日本人物之前休息吗?
在php手册中,在ord函数的注释中有一些UTF-8 ord函数的建议/代码。使用这样的函数,您可以通过字符串通过UTF-8代码点迭代UTF-8代码点,如果遇到ord为&gt;的代码点(字符)。中文/日文字符的开头,首先插入一个或其他任何内容。
编辑:ord的doc页面为here
这是我认为可能适合您的问题的代码:在shetline dot com引用作者kerry
这是我对之前发布的内容的看法 UTF-8版本的ord,适合 通过Unicode迭代字符串 值。该功能可以选择 将索引转换为字符串,然后 可选地返回字节数 由一个角色消耗,以便你 知道增加索引的数量 到达下一个角色。
<?php
function ordUTF8($c, $index = 0, &$bytes = null)
{
$len = strlen($c);
$bytes = 0;
if ($index >= $len)
return false;
$h = ord($c{$index});
if ($h <= 0x7F) {
$bytes = 1;
return $h;
}
else if ($h < 0xC2)
return false;
else if ($h <= 0xDF && $index < $len - 1) {
$bytes = 2;
return ($h & 0x1F) << 6 | (ord($c{$index + 1}) & 0x3F);
}
else if ($h <= 0xEF && $index < $len - 2) {
$bytes = 3;
return ($h & 0x0F) << 12 | (ord($c{$index + 1}) & 0x3F) << 6
| (ord($c{$index + 2}) & 0x3F);
}
else if ($h <= 0xF4 && $index < $len - 3) {
$bytes = 4;
return ($h & 0x0F) << 18 | (ord($c{$index + 1}) & 0x3F) << 12
| (ord($c{$index + 2}) & 0x3F) << 6
| (ord($c{$index + 3}) & 0x3F);
}
else
return false;
}
?>
答案 3 :(得分:0)
<?php
$str="我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him";
$splitPoints;
$indis=0;
for($i=0;$i<strlen($str);$i++){
if ($str[$i]=='['){
$splitPoints[$indis]=$i-4;
$indis++;
}
}
for($i=0;$i<$indis-1;$i++){
$strArray[$i]=substr($str,$splitPoints[$i],($splitPoints[$i+1]-$splitPoints[$i]));
}
$strArray[$i]=substr($str,$splitPoints[$indis-1],(strlen($str)-$splitPoints[$indis-1]));
for($i=0;$i<$indis;$i++){
echo $strArray[$i]."<br>";
}
?>