0

I've looked into the similar_text() and levenshtein() functions, but they only seem to return THAT there are similarities and the percentage of those similarities.

What I am trying to do is compare 2 strings to determine WHAT is actually similar between the two.

Basically:

<?php
$string1 = "IMG_1";
$string2 = "IMG_2";

echo CompareTheseStrings($string1,$string2);  // returns "IMG_";

If this wonderful function doesn't exist, what would be the best way to accomplish this?

My end game plan is to read through a list of file names and then replace the similar text with something user defined or just remove it all together, but I don't want to replace each files unique identifier.

MrPlentl
  • 11
  • 3
  • 3
    `implode(array_intersect_assoc(str_split($string1), str_split($string2)));` – Mark Baker Aug 18 '16 at 13:47
  • See http://stackoverflow.com/questions/321294/highlight-the-difference-between-two-strings-in-php – Wiktor Stribiżew Aug 18 '16 at 13:49
  • @MarkBaker: When the values are `$string1 = "New_IMG_1"; $string2 = "Oed_IMG_2";`, the result is [`e_IMG_`](https://ideone.com/Bwfc4A). – Wiktor Stribiżew Aug 18 '16 at 13:51
  • @MarkBaker Yea, that's a pretty "*hot-shot coder*" way of trying to solve [longest common substring](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) problem. It's also incorrect. – Sherif Aug 18 '16 at 13:56
  • @WiktorStribiżew - Yes, I never said it was a clever approach... even worse if you have something like `New_Img_1` and `Near_Img_1`.... but as the OP hasn't given any real idea of what their definition of similarity is, it's as bad an answer as any – Mark Baker Aug 18 '16 at 13:56
  • 1
    `doesn't exist` but there are others like: similar_text, levenshtein, soundex, metaphone,strcmp maybe you can built `CompareTheseStrings` from that. – JustOnUnderMillions Aug 18 '16 at 13:58
  • @WiktorStribiżew Good catch. That example would work otherwise. – MrPlentl Aug 18 '16 at 14:00

1 Answers1

-3

Reading your 'end goal' I think you're going about this completely the wrong way I think you should really be looking at str_replace

$string1 = "IMG_1";
$string2 = "IMG_2";

// you would create a loop here to insert each string
str_replace("IMG", "NEW_TERM", $string1);

If you want to remove the text altogether then just pass an empty string in as the 2nd parameter

Mark Twigg
  • 301
  • 1
  • 8
  • Correct. But the problem is that I am trying to solve for the first parameter. The text could be anything for "IMG". Once I know what the first parameter is, then I can execute the str_replace(). – MrPlentl Aug 18 '16 at 13:58
  • Just pass it a variable of what ever the user enters, if the user enters "MP3" then just pass that to str_replace as a variable str_replace($userInput, $newTerm, $string1) – Mark Twigg Aug 18 '16 at 14:00
  • I don't think "IMG" is a user input – Max Aug 18 '16 at 14:01
  • It doesn't matter what the user input is, it can be anything, I just used the term "IMG" because that's what he posted in the original question – Mark Twigg Aug 18 '16 at 14:04
  • Looking at the documentation, my problem it this: I know $subject and $replace... but I don't know what I am searching for. str_replace ( mixed $search , mixed $replace , mixed $subject) – MrPlentl Aug 18 '16 at 14:12
  • Why don't you know what you're searching for? The user inputs what they are searching for don't they? @MrPlentl – Mark Twigg Aug 18 '16 at 14:18
  • and what are the down votes for? this code is perfectly correct for what is trying to be achieved – Mark Twigg Aug 18 '16 at 14:23
  • @MarkTwigg I think you may have lost my main issue. The user would Input the replacement text, but they wouldn't know what they are replacing. Example, I go on a trip to Colorado and take a bunch of pictures. I get home, I put my pictures on the computer, but they are named DSC_01, DSC_02, etc. I want to rename all my pics Colorado-Trip-01, Colorado-Trip-02, etc. So I would enter "Colorado-Trip" and it would replace DSC_with that. I'm trying to create a solution that is a "catch all" for any naming, whether it is DSC_, IMG_, etc. This is a matter of the SCRIPT not knowing the variable "DSC_". – MrPlentl Aug 19 '16 at 15:18