Help with spliting array contents into words?
Posted: Sun Oct 02, 2005 10:43 am
Im having a problem with taking an array contents and splitting each element into words.
For example:
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:
And returns:
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
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");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);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 )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