jskidd3
jskidd3

Reputation: 4783

Compare and remove differences in 2 strings

I have two strings:

$stringOne = 'ABCDEF123-20140705-546.xml';
$stringTwo = 'ABCDEF123-20140705-589.xml';

I need the create a new string which looks like this: $result = 'ABCDEF123-20140705.xml'

The difference between the result and the two input strings is that the hyphen and 3 digit numbers at the end have been removed. I think this should be possible considering that the input strings are exactly the same other than the last 3 digits before the XML extension.

The reason I format the question this way is because there are hundreds of strings like this that I need to compare all with the same format, it's not as simple as just removing -546 and -589 from the strings as these are variables (as is the string prefix).

Any ideas?

Another two example strings could be:

$stringOne = 'XJ1ERJJFK-20140305-128.xml';
$stringTwo = 'XJ1ERJJFK-20140305-584.xml';

Which I'd like the result to be: $result = 'XJ1ERJJFK-20140305.xml'

Upvotes: 1

Views: 383

Answers (3)

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2125

If you're sure the string follows the correct format, use this:

function cutXmlFilePath($string) {
    return substr($string, 0, strrpos($string, '-')) . '.xml';
}

Sample usage:

$strings = array(
    'ABCDEF123-20140705-546.xml',
    'ABCDEF123-20140705-589.xml');
$result = array_map('cutXmlFilePath', $strings);
var_dump($result);

Outputs:

array (size=2)
  0 => string 'ABCDEF123-20140705.xml' (length=22)
  1 => string 'ABCDEF123-20140705.xml' (length=22)

Upvotes: 1

algorhythm
algorhythm

Reputation: 8728

Something like this?

use explode()

function cut($string)
{
    // divide filename in basename and extension (simple solution)  
    $filenameParts = explode('.', $string);
    $filename = $filenameParts[0];
    $extension = $filenameParts[1];

    $parts = explode('-', $filename); // split to an array
    unset($parts[2]); // remove the parts from array
    return implode('-', $parts) .'.' .$extension; // combine new filename
}

use preg_replace() (1)

Or shorter with a regex. This regex expects all the described components in the string. The first block with alpha and also the second block with the date.

function cut($string) {
    return preg_replace('/^([A-Z0-9]{9}\-[0-9]{8})(?:\-[0-9]{3})(\.xml)$/i', '$1$2', $string);
}

Regular expression visualization

Debuggex Demo

use preg_replace() (2)

Update: An other solution could be to just replace the string -NNN. where N is a number, like this:

function cut($string) {
    return preg_replace('/\-[0-9]{3}\./i', '.', $string);
}

Regular expression visualization

Debuggex Demo

And then call the function as often as you need:

$stringOne = 'XJ1ERJJFK-20140305-128.xml';
$stringTwo = 'XJ1ERJJFK-20140305-584.xml';

$stringOneNew = cut($stringOne); // returns "XJ1ERJJFK-20140305.xml"

For multiple string in an array do following:

$new_array = array();
foreach ($your_array as $filename) {
    $new_array[$filename] = cut($filename);
}

The resulting $new_array looks like following:

$new_array = array(
    // 'key' => 'value'
    // 'original' => 'generated with cut func'
    'ABCDEF123-20140705-546.xml' => 'ABCDEF123-20140705.xml',
    'ABCDEF123-20140705-589.xml' => 'ABCDEF123-20140705.xml',
    'XJ1ERJJFK-20140305-128.xml' => 'XJ1ERJJFK-20140305.xml',
    // ...
);

Now you have a mapping, your comparison string as array values and the original as key if necessary for later stuff. Then you can do many tricks with it, e.g.:

$unique_array_keys = array_keys($filenames); // array('ABCDEF123-20140705-546.xml', 'ABCDEF123-20140705-589.xml', 'XJ1ERJJFK-20140305-128.xml')
$unique_array_values = array_unique(array_values($filenames)); // array('ABCDEF123-20140705.xml', 'XJ1ERJJFK-20140305.xml')

// and more...

Upvotes: 3

webbiedave
webbiedave

Reputation: 48887

remove differences in 2 strings

Here is an example of how to do it. This will compare each character in the string and leave off differences, plus any trailing dashes in the filename. You may need to tweak it more to fit edge cases:

$str1 = 'XJ1ERJJFK-20140305-128.xml';
$str2 = 'XJ1ERJJFK-20140305-584.xml';

$pi1 = pathinfo($str1);
$pi2 = pathinfo($str2);

if ($pi1['extension'] === $pi2['extension']) {
    $newExt = '.' . $pi1['extension'];
} else {
    $newExt = '';
}

$piLen1 = strlen($pi1['filename']);

$newStr = '';
for ($i = 0; $i < $piLen1; $i++) {
    if ($pi1['filename'][$i] == $pi2['filename'][$i]) {
        $newStr .= $pi1['filename'][$i];
    }
}

$newStr = rtrim($newStr, '-') . $newExt;

echo $newStr; // XJ1ERJJFK-20140305.xml

Upvotes: 1

Related Questions