...

View Full Version : is it possible to check if part of a string matches a value in an array???



LJackson
02-04-2010, 05:46 PM
Hi All

i have a title
e.g xbox 360 assassins creed: part 2

and i am trying to find out if any part of that title matches an item in this array

$platform_array = array("Xbox 360", "PS3", "Wii", "DS", "PSP", "PS2");


which it does, then i want a varible to be assigned the value which is matches
e.g platform_result = xbox 360

not im not sure if the space will cause any problems and the fact that it needs to be case incensitive :)

here is the code i have tried

$platform_array = array("Xbox 360", "PS3", "Wii", "DS", "PSP", "PS2");
foreach($platform_array as $platform)
{
if(in_array($keywords,$platform))
{
$platform_result = $platform;
}
}
print $platform_result;


but nothing prints out, maybe because its trying to match the entire title which it does not find?

any help with this would be appreciated
thanks
Luke

JAY6390
02-04-2010, 05:54 PM
Try this

$platform_array = array('Xbox 360', 'PS3', 'Wii', 'DS', 'PSP', 'PS2');
$text = 'xbox 360 assassins creed: part 2';
$match = false;
foreach($platform_array as $v) {
if(preg_match('/'.preg_quote($v).'/i', $text)) {
$match = $v;
break;
}
}
if($match) {
echo $match;
}else{
echo 'No match found';
}

LJackson
02-04-2010, 06:07 PM
WOW thanks mate, works a treat !!!!

cheers
Luke

JAY6390
02-04-2010, 06:12 PM
No problem :)

LJackson
02-04-2010, 06:27 PM
and how would one check to see if there is an ":" in the title or string?

cheers
Luke

JAY6390
02-04-2010, 06:29 PM
as in the same method? you could add it to the array or just use strpos() (http://php.net/strpos)

LJackson
02-04-2010, 07:07 PM
of course :D how thick of me lol

cheers
Luke

LJackson
02-06-2010, 09:00 PM
ok i am now trying to check if some text is present in a string and if it is remove it

so i have this

switch($category['category'])
{
case "Desktop Computers";
$brand_array = array("HP","Packard Bell","Packard Bl","Compaq","Advent","Centurion","imedia");
$match = false;
foreach($brand_array as $v)
{
if(preg_match('/'.preg_quote($v).'/i', $prodName))
{
$platform = $v;
$short_title = preg_replace("/$platform/U", "", $prodName);
break;
}
}


which does work but it seems to be case sensitive

if i have a title COMPAQ 123THY LAPTOP using the above code would not remove COMPAQ because its Compaq in the array.

how do i make it so that it doesnt matter what the case is?

many thanks
Luke

JAY6390
02-06-2010, 09:04 PM
change the U in the second regex to i. It should be used with preg_quote too like the original

LJackson
02-06-2010, 09:22 PM
Cheers mate!!! appreciate it :)

LJackson
02-07-2010, 12:07 AM
one more question :) sorry :P

i have this code from an old project

$short_title = preg_replace("/.{3}(inch).*/", "", $short_title) . "\n";

which searches for the word inch and deletes everything after including the word inch and 3 characters before it, as this is slightly different to the other examples how would i set this as case insensitive? i tried

$short_title = preg_replace("/.{3}(inch).*/", "", $short_title) . "\n";

but no luck?

edit
====

ok had the /i in the wrong place the / was already in the expression just needed to add the i like so

$short_title = preg_replace("/.{3}(inch).*/i", "", $short_title) . "\n";

cheers
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum