How would i check if a variable contains a word in an array?
Thread moved by mat64 (Forum Super Moderator) from Designing & Development: Please post in the correct section
How would i check if a variable contains a word in an array?
Thread moved by mat64 (Forum Super Moderator) from Designing & Development: Please post in the correct section
Last edited by mat64; 21-01-2008 at 12:06 AM.
$array = array('words','to','search','for');
$string = 'words i would like to search';
if(in_array($string,$array)) {
//do something
}else{
//not in array
}
That doesn't making it if it contains it
For example if i put the word "hi" in the array, if the variable was just "hi" it would select it but if the variable was for example "lolhi" it wouldn't, i want it so if the word even just contains it.
Nevermind.
Edited by mat64 (Forum Super Moderator): Please don't post pointlessly
Last edited by mat64; 21-01-2008 at 12:07 AM.
try thatPHP Code:$array = array('words','to','search','for');
$string = 'words i would like to search';
$i=0;
$rules = '(';
while($i<count($array)) {
if($i!=count($array)) {
$rules .= $array[$i].'|';
}else{
$rules .= $array[$i];
}
$i++;
}
$rules .= ')';
if(preg_match("/".$rules."/",$string)) {
//Would create rule /(words|to|search|for)/
//do something
}else{
//do something else if not found
}
Id do..
PHP Code:<?php
$array = array("word1","word2","word3");
$string = "Variable string thing";
foreach($array as $v1)
{
if(eregi($string,$v1))
$i++;
}
if($i>0)
echo("This word is in the array!");
else
echo("It isnt in the array!");
?>
Coming and going...
Highers are getting the better of me
Want to hide these adverts? Register an account for free!