Page 1 of 1

Help with spliting array contents into words?

Posted: Sun Oct 02, 2005 10:43 am
by ron_j_m
Im having a problem with taking an array contents and splitting each element into words.
For example:

Code: Select all

//I have two arrays:
$dictionary = array ("test", "get", "me", "live", "it", "up", "best", "credit", "card", "processing");
$names = array("testm3eer", "getme", "34535-ddd", "liveitup", "get23it", "100best-credit-card-processing");

//I want to split the names array entries based on the dictionary array so I would end up with:
$result = array("test m3eer", "get me", "34535 ddd", "live it up", "get 23 it", "100 best credit card processing");

//But I end up with: (notice credit and cred it)
$result = array("test m3eer", "get me", "34535 ddd", "live it up", "get 23 it", "100 best cred it card processing");
The code I wrote seems to work ok but it cuts "credit" into "cred it". Both "credit" and "it" are in the dictionary array and I understand why its splitting it, I just need some help so it doesn't split "credit".

Heres what Im using:

Code: Select all

$i=0; 
while ($i<count($dictionary))
   {
  
foreach ($names as $k=>$string)
   {

$keywords =& $names[$k];
$keywords = preg_split("(($dictionary[$i]))", $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$keywords = preg_replace("/\-+/"," ", $keywords);
$keywords = implode(" ", $keywords);
   }
$i++;
}
print_r ($dictionary);
echo "<br>";
print_r ($names);
And returns:

Code: Select all

Array ( [0] => test [1] => get [2] => me [3] => live [4] => it [5] => up [6] => best [7] => credit [8] => card [9] => processing )
Array ( [0] => test m3eer [1] => get me [2] => 34535 ddd [3] => live it up [4] => get 23 it [5] => 100 best cred it card processing )
Im not verry good at php yet and It took me quite a while to come up with this, so if theres a better way of doing it, please share.
Anyone have any ideas on how to get the correct words, while keeping larger words intact? Keep in mind that the arrays may contain tens of thousands of entries from a db.
Thanks Wrench

Posted: Sun Oct 02, 2005 11:36 am
by s.dot
here's a shot in the dark

Code: Select all

foreach($dictionary AS $k =>$v)
{
   $joined[] = $v.' '.$names[$k];
}
print_r($joined);
Edit: Okay, nevermind.. I don't know what I was thinking... I think I only read half your post. =/

Also, your dictionary has the word 'it' in it. So that's why it's spacing at that point. Maybe you need a regex to determine if 'it' is by itself, or part of another word.

Posted: Sun Oct 02, 2005 11:39 am
by Jenk
You could remove "it" from the dictionary array, given that you have all the other words it should still split accordingly.

Posted: Sun Oct 02, 2005 11:52 am
by ron_j_m
The only problem is the dictionary array is just an example. The real dictionary is a row in a database that has tens of thousands of entries, and the names array, also from a database and thousands of entries, would be changing on a dialy basis.
Are there any sql commands that could handle this?