在不同位置之间提取和合并字符串

时间:2022-09-13 13:30:33

I'm trying to make this works. I want to replace some parts of a sentence between given positions and then show the full sentence with the changes. With the following code, I'm able to make the changes but I don't know how to put together the rest of the sentence.

我正在尝试使这项工作。我想在给定位置之间替换句子的某些部分,然后用变化显示完整的句子。使用以下代码,我可以进行更改,但我不知道如何将句子的其余部分组合在一起。

I have two arrays, one with the positions where a woman name appears and another one with men names. The code replaces the pronoun "his" by "her" when a woman is before a man between the intervals. The last thing I need is to reconstruct the sentence with the changes made but I don't know how to extract the rest of the sentence (the result, in the example, is from positions 0 to 20 (Maria her dress but) and 36 to 51 (Lorena her dog) but I need to extract from 20 to 36 (Peter his jeans) and 51 to the end (Juan his car) to merge them in their positions).

我有两个阵列,一个是女性名字出现的位置,另一个是男性名字。当一个女人在一个男人之间的间隔之前,代码用“她”代替代词“他的”。我需要的最后一件事就是用所做的改变来重建句子,但我不知道如何提取句子的其余部分(结果,在示例中,是从0到20位置(玛丽亚她的衣服但是)和36到51(Lorena,她的狗),但我需要从20到36(彼得他的牛仔裤)和51到最后(胡安他的车)提取合并他们的位置)。

The result should be: "Maria her dress but Peter his jeans Lorena her dog Juan his car". I'll appreciate any help with this, I've been looking for other similar questions but I found nothing.

结果应该是:“玛丽亚她的衣服,但彼得他的牛仔裤洛雷娜她的狗胡安他的车”。我会感激任何帮助,我一直在寻找其他类似的问题,但我一无所获。

<?php

$womenpos  = array("0","36"); //these arrays are just examples of positions
$menpos    = array("20","51"); //they will change depending on the sentence
$sentence  = "Maria his dress but Peter his jeans Lorena his dog Juan his car";

echo $sentence."\n";      


foreach ($womenpos as $index => $value) {
    $value2 = $menpos[$index];      

    if($value < $value2) {
        echo "\nWoman(" . $value . ") is before man(" . $value2 . ")\n";
        $end = ($value2 - $value);
        $improved = str_replace(' his ', ' her ', 
                         substr($sentence, $value, $end));                           

        echo $improved."\n";                 

    } else {
        $improved = "Nothing changed";
     echo $improved;
    }     

}

2 个解决方案

#1


Ok, how about this:

好的,这个怎么样:

$womenpos  = array("0","36");
$menpos    = array("20","51");
$bothpos   = array_merge($womenpos,$menpos);
sort ($bothpos);
print_r($bothpos);

$sentence  = "Maria his dress but Peter his jeans Lorena his dog Juan his car";

echo $sentence."\n";      

for ($i = 0; $i<sizeof($bothpos); $i++) {
    $start = $bothpos[$i];
    if ($i ==sizeof($bothpos)-1) {
        $end = strlen($sentence);
    }
    else {
        $end = $bothpos[$i+1];
    }
    $length = $end-$start;
    $segment = substr($sentence, $start, $length);
    if (in_array($start, $womenpos)) {
        $new_segment = str_replace (' his ', ' her ', $segment);
    }
    else { $new_segment = $segment; }
    $improved .= $new_segment; 
    print "<li>$start-$end: $segment :  $new_segment </li>\n";
}

print "<p>Improved: $improved</p>";

This combines the men's and women's position arrays to consider each stretch of text as one that might have an error. If that stretch of text starts at one of the womenpos points, then it changes 'his' to 'her'. If not it leaves it alone.

这结合了男性和女性的位置数组,将每一段文本视为可能出错的文本。如果那段文字从其中一个女性点开始,那么它将“他的”改为“她”。如果没有,它就不管它了。

Does this get you in the direction you want to go in? I hope so!

这会让你朝着你想要的方向前进吗?希望如此!

#2


This approaches the problem differently, but I wonder if it would provide the solution you're looking for:

这会以不同的方式解决问题,但我想知道它是否会提供您正在寻找的解决方案:

$sentence  = "Maria his dress but Peter his jeans Lorena his dog Juan his car";

$women = array ("Maria", "Lorena");

$words = explode (" ", $sentence);

for ($i=0; $i< sizeof($words); $i++) {
    if ($words[$i] == "his" && in_array($words[$i-1], $women)) {
        $words[$i] = "her";
    }
}

print (join(" ", $words));

This goes through the words one at a time; if the preceding word is in the $women array and the current word is "his", it changes the word to "her". Then it spits out all the words in order.

这经历了一次一个词;如果前面的单词在$ women数组中并且当前单词是“his”,则它将单词更改为“her”。然后它按顺序吐出所有单词。

Does this do what you need, or do you really want a complex string positioning answer?

这样做你需要的,还是你真的想要一个复杂的字符串定位答案?

#1


Ok, how about this:

好的,这个怎么样:

$womenpos  = array("0","36");
$menpos    = array("20","51");
$bothpos   = array_merge($womenpos,$menpos);
sort ($bothpos);
print_r($bothpos);

$sentence  = "Maria his dress but Peter his jeans Lorena his dog Juan his car";

echo $sentence."\n";      

for ($i = 0; $i<sizeof($bothpos); $i++) {
    $start = $bothpos[$i];
    if ($i ==sizeof($bothpos)-1) {
        $end = strlen($sentence);
    }
    else {
        $end = $bothpos[$i+1];
    }
    $length = $end-$start;
    $segment = substr($sentence, $start, $length);
    if (in_array($start, $womenpos)) {
        $new_segment = str_replace (' his ', ' her ', $segment);
    }
    else { $new_segment = $segment; }
    $improved .= $new_segment; 
    print "<li>$start-$end: $segment :  $new_segment </li>\n";
}

print "<p>Improved: $improved</p>";

This combines the men's and women's position arrays to consider each stretch of text as one that might have an error. If that stretch of text starts at one of the womenpos points, then it changes 'his' to 'her'. If not it leaves it alone.

这结合了男性和女性的位置数组,将每一段文本视为可能出错的文本。如果那段文字从其中一个女性点开始,那么它将“他的”改为“她”。如果没有,它就不管它了。

Does this get you in the direction you want to go in? I hope so!

这会让你朝着你想要的方向前进吗?希望如此!

#2


This approaches the problem differently, but I wonder if it would provide the solution you're looking for:

这会以不同的方式解决问题,但我想知道它是否会提供您正在寻找的解决方案:

$sentence  = "Maria his dress but Peter his jeans Lorena his dog Juan his car";

$women = array ("Maria", "Lorena");

$words = explode (" ", $sentence);

for ($i=0; $i< sizeof($words); $i++) {
    if ($words[$i] == "his" && in_array($words[$i-1], $women)) {
        $words[$i] = "her";
    }
}

print (join(" ", $words));

This goes through the words one at a time; if the preceding word is in the $women array and the current word is "his", it changes the word to "her". Then it spits out all the words in order.

这经历了一次一个词;如果前面的单词在$ women数组中并且当前单词是“his”,则它将单词更改为“her”。然后它按顺序吐出所有单词。

Does this do what you need, or do you really want a complex string positioning answer?

这样做你需要的,还是你真的想要一个复杂的字符串定位答案?