2
$mymarker = '/MARKER[0-9]{2}/';
preg_match_all($mymarker, $mycontent, $matches);
var_dump($matches);

Gives:

array(1) {
  [0]=>
  array(18) {
    [0]=>
    string(8) "MARKER00"
    [1]=>
    string(8) "MARKER01"
    [2]=>
    string(8) "MARKER02"
    [3]=>
    string(8) "MARKER04"
    [4]=>
    string(8) "MARKER05"
    [5]=>
    string(8) "MARKER07"
    [6]=>
    string(8) "MARKER09"
    [7]=>
    string(8) "MARKER13"
    [8]=>
    string(8) "MARKER13"
    [9]=>
    string(8) "MARKER16"
    [10]=>
    string(8) "MARKER15"
    [11]=>
    string(8) "MARKER21"
    [12]=>
    string(8) "MARKER31"
    [13]=>
    string(8) "MARKER22"
    [14]=>
    string(8) "MARKER24"
    [15]=>
    string(8) "MARKER26"
    [16]=>
    string(8) "MARKER80"
    [17]=>
    string(8) "MARKER81"
  }
}

As we see, MARKER13 occurs twice. This duplicate instance can occur with MARKER05, MARKER07 and MARKER13.

The second value is needed to be updated to MARKERXX+1 so Second occurrence of:

Marker05 will have to be updated to MARKER06 Marker07 will have to be updated to MARKER08 MARKER13 will have to be updated to MARKER14.

How can we set this up in a loop, to check for dups and update the duplicate value to the next value.

Thanks

1 Answer 1

2

There might be easier ways, but you could come up with the following

<?php
$matches = array("MARKER00","MARKER01","MARKER02","MARKER04","MARKER05","MARKER07","MARKER09","MARKER13","MARKER13","MARKER16","MARKER15","MARKER21","MARKER31","MARKER22","MARKER24","MARKER26","MARKER80","MARKER81");

$duplicates = array("MARKER05", "MARKER07", "MARKER13");
$len = count($matches);

for ($i=0;$i<$len;$i++) {
    $marker = $matches[$i];

    if (in_array($marker, $duplicates)) {
        // one of our markers
        // check if the next is the same
        // if so update it
        if ($i<$len-1) {
            if ($marker == $matches[$i+1]) {
                # split the string into a letter and a digit part
                # with lookarounds (behind/ahead, both positive)
                list($text,$number) = preg_split('~(?<=[A-Z])(?=\d)~', $matches[$i+1]);
                $number = intval($number);
                # increase the intval'd number by one
                # and apply it to the original array
                $number += 1;
                $matches[$i+1] = $text . $number;
            }
        }
    }
}

print_r($matches);
// updated to MARKER14

?>

Apparently, this works only if the array is ordered, ie duplicates follow up.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Your reply gave me an idea of avoiding the loop part. MARKERXX are positions in html document that I am splitting on MARKERXX and next MARKERXX in sequence (XX can be any number and not necessarily ordered) to map to db column. MARKERXX are obtained thru preg_replace. So, since only fixed (3) numbers of markers could repeat across the entire files set, I ran the preg_replace($patterns, $replacements, 1) twice once with 05, 07, 13 and next time with 06, 08 and 14 and got non repeating MARKERSXX in the array. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.