反转句子中的“英语”单词

时间:2018-11-28 09:27:25

标签: php regex preg-match

我有一个字符串,其中可能使用多种语言,例如:

این متن فارسی است 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

4 个答案:

答案 0 :(得分:3)

您可以使用

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匹配一个或多个不属于阿拉伯语脚本且不属于CR和LF的连续字符(以非空白字符开头)。匹配项将传递给preg_replace_callback匿名函数,在该匿名函数中,匹配项首先与另一个preg_replace_callback处理,以反转以字母开头和标点符号结尾的单词中的字母和标点符号(请参见(\w+)([^\w\s]+) regex)然后使用任何种类的Unicode空格进行拆分,同时丢弃空项目(请参见preg_split('~\s+~u', $m[0], -1, PREG_SPLIT_NO_EMPTY)),array_reverse反转找到的“单词”数组,而implode将反转的项目与一个空格连接起来。

答案 1 :(得分:1)

使用反向数组

class Book(models.Model):
    publisher = models.IntegerField()
    issue = models.CharField(max_length=255)

    class Meta:
        unique_together = ("publisher", "issue")

    def __str__(self):
        return "%s-%r" % (self.publisher,self.issue)


class Version(models.Model):
    book = models.ForeignKey(Book, on_delete = models.CASCADE)
    number = models.CharField(max_length=255)
    book_name = models.CharField(max_length=255)
    cover = models.ImageField(null=True)
    description = models.CharField(max_length=255)

    class Meta:
        unique_together = ("book", "number")

    def __str__(self):
        return "%s %s" % (self.number, self.book)




class Copy(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
    version = models.ForeignKey(Version, on_delete = models.CASCADE)
    number = models.IntegerField()
    book_id = models.AutoField(primary_key=True)
    book_size = models.CharField(null=True, blank=True,max_length=255)


    def __str__(self):
        return "%r" % (self.book_id)


    class Meta:
        unique_together = ("version", "number")

答案 2 :(得分:1)

您需要选择以英文字符开头和以英文字符结尾的字符串。然后在回调函数中使用array_reverse()

将字符串按空格和反向数组拆分
$newStr = preg_replace_callback('/\w.*(?=\w|\b)/', function (array $m) { 
    return implode(" ", array_reverse(explode(" ", $m[0])));
}, $string);

demo中查看结果

答案 3 :(得分:1)

尝试一下:

<?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