Jump to content

is there a way to compare two strings, and generate a universal regex expresion


Recommended Posts

Depends.  Are you just trying see what's in common from the beginning to X or, are you trying to compare all common characters?  Is the end always a number?

 

For the first part you could use something like:

 

$s1 = "apple1";
$s2 = "apple2";
$i = 0;
$temp = "";
while($s1[$i] != null)
{
   if($s1[$i] == $s2[$i])
   {
      $temp .= $s1[$i];
   }
   $i++;
}

echo $temp;
?>

well for example take URL

 

$d = "www.youtube.com/watch?v=AWm2zGTZBM0&feature=topvideos";
$d1 = "www.youtube.com/watch?v=vasdf32v&feature=featured";

a regex should be generated that would match that structure.

 

for instance it should out put $regex

"/www.youtube.com/watch?v=[a-zA-Z0-9]&feature=[a-z]/i

is it assured that the two examples given to the function you want to make will have everything that is possible to be different be different? otherwise this will not be possible, as it cannot possibly know what elements happen to be the same with the examples, and might in another case be different.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.