将"English"个字反转造句
Reverse "English" words in a sentence
我有一个字符串,其中可能使用了多种语言,例如:
این متن فارسی است and this is !!! in English
این خط بعدی است و this is going to be continued...
我只想反转英文单词,所以结果应该是这样的:
این متن فارسی است English in !!! is this and
این خط بعدی است و continued be to going is this...
对于要反转的文本,我得到:
public static function reverseEnglishWords($string)
{
return preg_replace_callback('/\w+/', function (array $m) { return strrev($m[0]); }, $string);
}
但它反转了所有字符。
编辑:
这是目前最有效的方法:
<?php
$string = "
این یک تست جدید است
Is my English line working just fine?
I hope it does...
این یک جمله جدید است and this is to be continued in English...
";
$newStr = preg_replace_callback('/\w.*(?=\w|\b)/', function (array $m) {
return implode(" ", array_reverse(explode(" ", $m[0])));
}, $string);
echo $newStr;
唯一的问题是字符串 enf 中的某些字符,如 ?
和 ...
在上面的示例中放错了位置。当前结果是:
این یک تست جدید است
fine just working line English my Is?
does it hope I...
این یک جمله جدید است English in continued be to is this and...
应该是:
این یک تست جدید است
?fine just working line English my Is
...does it hope I
این یک جمله جدید است ...English in continued be to is this and
使用数组反转
$str = 'what is you doin';
$str = explode(' ', $str);
$str = array_reverse($str);
echo implode(' ', $str);
您需要 select 以英文字符开头并以英文字符结尾的字符串。然后在回调函数中通过 space 拆分字符串并使用 array_reverse()
反转数组
$newStr = preg_replace_callback('/\w.*(?=\w|\b)/', function (array $m) {
return implode(" ", array_reverse(explode(" ", $m[0])));
}, $string);
检查结果 demo
试试这个:
<?php
function reverseSentence($string)
{
$array = explode(" ", $string);
$rarray = array_reverse($array);
$reversed = implode(" ", $rarray);
return $reversed;
}
function reverseEnglishWords($string)
{
return preg_replace_callback('/[\s\w\d]*/', function($m) {
return reverseSentence($m[0]);
} , $string);
}
$s = "این متن فارسی است and this is !!! in English";
echo reverseEnglishWords($s);
输出:
> $ php example.php
این متن فارسی است is this and !!!English in
您可以使用
function reverseEnglishWords($string)
{
return preg_replace_callback('/(?!\s)[^\p{Arabic}\r\n]+/u', function ($m) {
$tmp = preg_replace_callback('~(\w+)([^\w\s]+)~', function($n) {
return strrev($n[2]) . $n[1];
}, $m[0]);
return implode(" ", array_reverse(
preg_split('~\s+~u', $tmp, -1, PREG_SPLIT_NO_EMPTY)
)
);
}, $string);
}
$s = "
این یک تست جدید است
Is my English line working just fine?
I hope it does...
Another one?!
این یک جمله جدید است and this is to be continued in English...
";
echo reverseEnglishWords($s);
见PHP demo,输出
این یک تست جدید است
?fine just working line English my Is
...does it hope I
!?one Another
این یک جمله جدید است ...English in continued be to is this and
这里,/(?!\s)[^\p{Arabic}\r\n]+/u
matches one or more consecutive chars (starting with a non-whitespace char) that do not belong to the Arabic script and that are not CR and LF. The matches are passed to the preg_replace_callback
anonymous function where the match is first processed with another preg_replace_callback
to reverse letters and punctuation in words that start with letters and end with punctuation (see (\w+)([^\w\s]+)
正则表达式)然后用任何一种 Unicode whitespace 拆分,同时丢弃空项(参见 preg_split('~\s+~u', $m[0], -1, PREG_SPLIT_NO_EMPTY)
),array_reverse
反转数组"words" 找到,implode
将反向项目与 space 连接起来。
我有一个字符串,其中可能使用了多种语言,例如:
این متن فارسی است and this is !!! in English
این خط بعدی است و this is going to be continued...
我只想反转英文单词,所以结果应该是这样的:
این متن فارسی است English in !!! is this and
این خط بعدی است و continued be to going is this...
对于要反转的文本,我得到:
public static function reverseEnglishWords($string)
{
return preg_replace_callback('/\w+/', function (array $m) { return strrev($m[0]); }, $string);
}
但它反转了所有字符。
编辑: 这是目前最有效的方法:
<?php
$string = "
این یک تست جدید است
Is my English line working just fine?
I hope it does...
این یک جمله جدید است and this is to be continued in English...
";
$newStr = preg_replace_callback('/\w.*(?=\w|\b)/', function (array $m) {
return implode(" ", array_reverse(explode(" ", $m[0])));
}, $string);
echo $newStr;
唯一的问题是字符串 enf 中的某些字符,如 ?
和 ...
在上面的示例中放错了位置。当前结果是:
این یک تست جدید است
fine just working line English my Is?
does it hope I...
این یک جمله جدید است English in continued be to is this and...
应该是:
این یک تست جدید است
?fine just working line English my Is
...does it hope I
این یک جمله جدید است ...English in continued be to is this and
使用数组反转
$str = 'what is you doin';
$str = explode(' ', $str);
$str = array_reverse($str);
echo implode(' ', $str);
您需要 select 以英文字符开头并以英文字符结尾的字符串。然后在回调函数中通过 space 拆分字符串并使用 array_reverse()
$newStr = preg_replace_callback('/\w.*(?=\w|\b)/', function (array $m) {
return implode(" ", array_reverse(explode(" ", $m[0])));
}, $string);
检查结果 demo
试试这个:
<?php
function reverseSentence($string)
{
$array = explode(" ", $string);
$rarray = array_reverse($array);
$reversed = implode(" ", $rarray);
return $reversed;
}
function reverseEnglishWords($string)
{
return preg_replace_callback('/[\s\w\d]*/', function($m) {
return reverseSentence($m[0]);
} , $string);
}
$s = "این متن فارسی است and this is !!! in English";
echo reverseEnglishWords($s);
输出:
> $ php example.php
این متن فارسی است is this and !!!English in
您可以使用
function reverseEnglishWords($string)
{
return preg_replace_callback('/(?!\s)[^\p{Arabic}\r\n]+/u', function ($m) {
$tmp = preg_replace_callback('~(\w+)([^\w\s]+)~', function($n) {
return strrev($n[2]) . $n[1];
}, $m[0]);
return implode(" ", array_reverse(
preg_split('~\s+~u', $tmp, -1, PREG_SPLIT_NO_EMPTY)
)
);
}, $string);
}
$s = "
این یک تست جدید است
Is my English line working just fine?
I hope it does...
Another one?!
این یک جمله جدید است and this is to be continued in English...
";
echo reverseEnglishWords($s);
见PHP demo,输出
این یک تست جدید است
?fine just working line English my Is
...does it hope I
!?one Another
این یک جمله جدید است ...English in continued be to is this and
这里,/(?!\s)[^\p{Arabic}\r\n]+/u
matches one or more consecutive chars (starting with a non-whitespace char) that do not belong to the Arabic script and that are not CR and LF. The matches are passed to the preg_replace_callback
anonymous function where the match is first processed with another preg_replace_callback
to reverse letters and punctuation in words that start with letters and end with punctuation (see (\w+)([^\w\s]+)
正则表达式)然后用任何一种 Unicode whitespace 拆分,同时丢弃空项(参见 preg_split('~\s+~u', $m[0], -1, PREG_SPLIT_NO_EMPTY)
),array_reverse
反转数组"words" 找到,implode
将反向项目与 space 连接起来。