1

I have two strings

$Str1 = "A B C D E F G H I J";
$Str2 = "1 2 C D E P Q R";

Now I have to check that a substring of $Str2 exits in $Str1. For example in this case C D E.

This is a basic and simple example. I am having complex cases and data in both strings is dynamic.

If I would had to compare complete string the strpos was the solution but what to do in this case?

3
  • 1
    Does order matter? Is a char in a different location still a match? Do you need to return the specific chars and their locations? What about space chars - they clearly exist in both strings, but im guessing you dont count those? Is a single char a match, or do you need to find strings with a minimum length? As it stands this question is too vague to answer Commented Oct 14, 2015 at 14:00
  • @Steve, Position doesn't matter. I just need if substring of string exits as a substring in other string. And I have to match complete word(s). This is a basic example, I am having actual text in both strings. In my string there won't be chars but words and two words of a string should be together in other string if I am checking for those two words. Commented Oct 14, 2015 at 14:07
  • 1
    Ok well perhaps you need to edit your question with a more realistic example, because at the moment its really unclear. Commented Oct 14, 2015 at 14:11

3 Answers 3

2

This function should work any Strings

function test($str1, $str2)
{
    for ($i = strlen($str1); $i != 0; $i--) {
        for ($x = 0; $x < strlen($str1) - $i; $x++) {
            if (($pos = strpos($str2, substr($str1, $x, $i + 1))) !== false) {
                return substr($str1, $x, $i + 1);
            }
        }
    }
    return null;
}

It returns the biggest possible match or null.

$str1 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
$str2 = "Sed ut perspiciatis undeT omnis iste natus error sit voluptatem";
echo test($str1, $str2); // omnis iste natus error sit voluptatem
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Just what I required.
1

This function should work for you:

<?php
$Str1 = "A B C D E F G H I J";
$Str2 = "1 2 C D E P Q R";

echo strCompare($Str1, $Str2); //Returns C D E

function strCompare($Str1, $Str2) {
    $str1 = explode(" ", $Str1);
    $str2 = explode(" ", $Str2);

    $match = false;
    $newStr = [];

    foreach($str1 as $key => $value){
        if($match == true){
            if(isset($str2[$key]) && $str2[$key] == $value){
                $newStr[] = $value;
            } else {
                break;
            }
        } else {
            if(isset($str2[$key]) && $str2[$key] == $value){
                $match = true;
                $newStr[] = $value;
            }   
        }
    };

    return implode(" ",$newStr);

}

2 Comments

I tried but it was not working. Solution provided by Kanti worked exactly in the way I required. Thanks for the help though :)
If this doesn't work then you have a very old PHP version (PHP 5.4<). You should upgrade it.
0

maybe this one might help too:

<?php
/**
 * returns an array of positions and substrings of needle that were found 
 * in string haystack using a minimal and maximal substring length
 * 
 * @param unknown $haystack find substrings of needle in this string
 * @param unknown $needle substrings in needle will be searched in haystack
 * @param number $min minimal length of a substring
 * @param number $max maximum length of a substring
 * @return array empty if no result
 */
function intersect_string($haystack,$needle,$min=3,$max=25)
{
    $r = [];
    for($j=$i=0;isset($needle[$i])&&isset($needle[$i+1]);$j=0,$i++) {
        if (!($c=$needle[$i].$needle[$i+1])) continue;
        while (($p=strpos($haystack,$c,$j))!==false) {
            $r[$p] = $c;
            for ($j=$p+2,++$i;isset($needle[$i+1])&&isset($haystack[$j]);$j++,$i++) {
                if (!($needle[$i+1]==$haystack[$j])) break;
                $r[$p] .= $haystack[$j];
            }
            $l = strlen($r[$p]);
            if ($l<$min||$l>$max) { unset($r[$p]); }
        }
    }
    return $r;
}
header('Content-Type:text/plain');
$str1 = "A little wolf is jumping over the tiny cow";
$str2 = "A tiny cow is jumping over the little wolf";
var_dump(intersect_string($str1,$str2,2));
var_dump(intersect_string($str1,$str2,3,12));
$str1 = "A B C D E F G H I J";
$str2 = "1 2 C D E P Q R";
var_dump(intersect_string($str1,$str2,1));
var_dump(intersect_string($str1,$str2,8));
$str1 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
$str2 = "Sed ut perspiciatis undeT omnis iste natus error sit voluptatem";
var_dump(intersect_string($str1,$str2,3,40));
?>

this will result in:

array(4) {
  [0]=>
  string(2) "A "
  [34]=>
  string(8) "tiny cow"
  [13]=>
  string(21) " is jumping over the "
  [2]=>
  string(11) "little wolf"
}
array(2) {
  [34]=>
  string(8) "tiny cow"
  [2]=>
  string(11) "little wolf"
}
array(1) {
  [3]=>
  string(7) " C D E "
}
bool(false)
array(2) {
  [0]=>
  string(24) "Sed ut perspiciatis unde"
  [24]=>
  string(38) " omnis iste natus error sit voluptatem"
}

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.