Identifying common sequences in two strings
Posted: Tue Oct 23, 2007 12:19 am
I've got a problem that just surfaced, and I can't immediately see an easy solution... I'm hoping I'm over-thinking this and that someone can see this a different way.
Given two strings:
Function f($foo, $bar) returns an array of continuous sequences of 2 or more characters that are common to both strings:
...in any order. I'm not concerned that it's implemented exactly like this, just that I can identify common sequences of variable length >1. Not certain if this is a regexp solution or not, so I'll post here for now. Can someone shove me in the right direction?
Given two strings:
Code: Select all
$foo = "The name of the cat is Sprinkles. She likes balls of yarn, and has a hairball problem";
$bar = "The old lady knits a cat with a ball of yarn, enjoys sprinkles on her ice cream, and has a hairball problem";Code: Select all
array('the', 'cat', 'sprinkles', 'of yarn', 'and has a hairball problem') // these would naturally include leading/trailing spaces, but pretend i trimmed them