1

I have an array like so:

   array{
    [39] => src="http://www.google.com/jsapi">
    [111] => src="http://www.example.com/wp-content/themes/code.js"
    [55] => src="http://www.example.com/wp-content/themes/yui.js"
    [1015] => src="wp-content/uploads/website.jpg"
   }

Qeustion

What is the best way to find the difference of string ?

What I mean is to check if there are any duplicates.

src="http://www.example.com/wp-content/themes/ is repeated twice.

so if I then had an array like this:

array{
    [39] => src="http://www.google.com/jsapi">
    [55] => src="http://www.example.com/wp-content/themes/yui.js"
    [1015] => src="wp-content/uploads/website.jpg"
   }

http://www. is repeated twice.

so the Idea is to compare the string with another and find something that is duplicated and push it to an array ... thats all that I need :)

I am not saying it could be possible, but it would be nice if it could be :)

11
  • Can you clarify a little bit more what you need. Have you tried with regular expressions? You can match duplicates and select them as groups to work on them later... Commented Jul 18, 2011 at 16:37
  • Are you matching only from the beginning? Commented Jul 18, 2011 at 16:38
  • So you want to determine unique string prefixes? strncmp? Commented Jul 18, 2011 at 16:40
  • @mellamokb It may not always be the case, [if on second array] google had secure https: then the result would be ://www. is a duplicate :) Commented Jul 18, 2011 at 16:40
  • 1
    What about js duplicated in jsapi and yui.js? Commented Jul 18, 2011 at 16:41

3 Answers 3

2

Community, feel free to optimize the code. I quickly wrote this out.

function findStrDuplicate($str1, $str2)
{
    $duplicate = '';
    for($i = 0; $i < min(strlen($str1), strlen($str2)); $i++) {
        if($str1[$i] != $str2[$i]) {
            break;
        }
        $duplicate .= $str1[$i];
    }

    return $duplicate;
}

function findArrayPrefixDuplicate($array)
{
    $duplicatesArray = array();

    for($i = 0; $i < count($array) - 1; $i++) {
        for($j = $i+1; $j < count($array); $j++) {
            $dup = findStrDuplicate($array[$i], $array[$j]);
            if(!empty($dup)) {
                $duplicatesArray[] = $dup;
            }
        }
    }

    return array_unique($duplicatesArray);
}

$data = array(
    'src="http://www.google.com/jsapi"',
    'src="http://www.example.com/wp-content/themes/code.js"',
    'src="http://www.example.com/wp-content/themes/yui.js"',
    'src="wp-content/uploads/website.jpg"'
);

// Remove src="..."

$data = array_map(function ($val) {
    return substr($val, 5, strlen($val) - 1);
}, $data);

$result = findArrayPrefixDuplicate($data);

var_dump($result);

Result:

array
  0 => string 'http://www.' (length=11)
  2 => string 'http://www.example.com/wp-content/themes/' (length=41)
Sign up to request clarification or add additional context in comments.

Comments

0

The following link should help with this: http://www.rogerethomas.com/b/14/php-search-string-for

You just need to change the else output to add it to the array and add a while loop in there if you are reiterating over multiple entries

2 Comments

If I knew the $x there wouldn't be any point asking the question as I have lol, str_replace(); would have done the job :)
oh, you are looking at finding the diff and then using that in order to find the dupe, sorry for the confusion
0

I think that you will probably want to take a look at the suffix tree data structure. Take a look at this post for some ideas:

Fast String Search With Suffix Trees

Comments

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.