Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get the difference string after comparing

Status
Not open for further replies.

hunt00

Technical User
Mar 6, 2005
79
0
0
US
How do i get the different string after compare

1. string1 = this-is-a-test
2. string2 =this-is-a-test-numberOne

how to compare those strings and get the string "numberOne" as the result?

Thanks very much for your help!
 
It looks like this will do what you want.

PHP:
$str1 = "this-is-a-test";
$str2 = "this-is-a-test-numberOne";

$result = strcmp($str1,$str2);

switch (TRUE) {
  case ($result < 0):
    $remainder = substr($str2,$result);
    echo "The extra text was: $remainder<br />\n";
    break;
  case ($result > 0):
    $remainder = substr($str1,($result * -1));
    echo "The extra text was: $remainder<br />\n";
    break;
  default:
    echo "Strings were the same, no extra data<br />\n";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top