jjk2 Posted May 2, 2009 Share Posted May 2, 2009 is there a way to compare two strings, and generate a universal regex expresion ? for instance take "Apple1" & "Apple2" and output $regex = /apple\d+/. Quote Link to comment https://forums.phpfreaks.com/topic/156469-is-there-a-way-to-compare-two-strings-and-generate-a-universal-regex-expresion/ Share on other sites More sharing options...
Maq Posted May 2, 2009 Share Posted May 2, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156469-is-there-a-way-to-compare-two-strings-and-generate-a-universal-regex-expresion/#findComment-823928 Share on other sites More sharing options...
jjk2 Posted May 2, 2009 Author Share Posted May 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156469-is-there-a-way-to-compare-two-strings-and-generate-a-universal-regex-expresion/#findComment-823930 Share on other sites More sharing options...
hchsk Posted May 2, 2009 Share Posted May 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156469-is-there-a-way-to-compare-two-strings-and-generate-a-universal-regex-expresion/#findComment-823961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.