Search code examples
phparrayssubstr

Extract and merge strings between different positions


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).

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;
    }     

}

Solution

  • 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!