PDA

View Full Version : How would i



RyanFTW
20-01-2008, 11:19 PM
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

Baving
20-01-2008, 11:20 PM
$array = array('words','to','search','for');
$string = 'words i would like to search';
if(in_array($string,$array)) {
//do something
}else{
//not in array
}

RyanFTW
20-01-2008, 11:28 PM
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.

Invent
20-01-2008, 11:41 PM
Nevermind.

Edited by mat64 (Forum Super Moderator): Please don't post pointlessly

Baving
21-01-2008, 05:32 PM
$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

}


try that

MrCraig
21-01-2008, 06:53 PM
Id do..



<?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!");
?>

Want to hide these adverts? Register an account for free!