Help with spliting array contents into words?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ron_j_m
Forum Commoner
Posts: 35
Joined: Wed Feb 02, 2005 8:56 pm

Help with spliting array contents into words?

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You could remove "it" from the dictionary array, given that you have all the other words it should still split accordingly.
ron_j_m
Forum Commoner
Posts: 35
Joined: Wed Feb 02, 2005 8:56 pm

Post 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?
Post Reply