Page 1 of 1
Part of a string.
Posted: Sun Nov 23, 2008 4:53 pm
by Suudsu
Say I had this:
Code: Select all
$value= array("Cristopher");
if(in_array($x,$value)){
echo "Yes";}
else{ echo "No";}
Now if $x is equal to "Christopher" then it will echo "Yes".
But how can I make it so if $x="Chris" it is also "Yes"? And what about "Christoph" or any other part of Christopher ?
Re: Part of a string.
Posted: Sun Nov 23, 2008 4:55 pm
by VladSun
substr()
array_filter()
array_walk()
Re: Part of a string.
Posted: Sun Nov 23, 2008 5:01 pm
by Suudsu
Forgive me if I misunderstand what you said, but it appears as if that wouldn't work the way I want it to. I want it so that if X$ is equal to something like "Chr" or "Er" it will return yes. They things you posted appear as if I could only get them to work if I checked every possible combination of the word. If I'm wrong could you please show me how?
-Thanks
Re: Part of a string.
Posted: Sun Nov 23, 2008 5:28 pm
by VladSun
Suudsu wrote:if I checked every possible combination of the word. If I'm wrong could you please show me how?
http://bg.php.net/substr
Re: Part of a string.
Posted: Sun Nov 23, 2008 5:37 pm
by Suudsu
I'm sorry but I still don't understand. Are you saying that I have to check every possible combination, and should do it using substr?
That would seem rather cumbersome. Or am I missing the point all together?
-Thanks
Re: Part of a string.
Posted: Sun Nov 23, 2008 6:00 pm
by Suudsu
Ok, I kind of got it.
My main purpose of this was to make a kind of search. The program would take a string, and if the word was in that sting, it would say "yes", and if not, it would say "No". Is there some way to use strstr() to do that? I assume I could do something with
Code: Select all
If ([i]the return of the string [/i]!= "FALSE")
But how would I do that?
Re: Part of a string.
Posted: Sun Nov 23, 2008 6:06 pm
by kaisellgren
Code: Select all
$value= array("Cristopher");
array_walk($value,'myfunction');
function myfunction($item)
{
if (stripos($item,'cr') !=== false)
echo 'yes';
else
echo 'no';
}
didn't test..
Re: Part of a string.
Posted: Sun Nov 23, 2008 6:20 pm
by Suudsu
kaisellgren wrote:Code: Select all
$value= array("Cristopher");
array_walk($value,'myfunction');
function myfunction($item)
{
if (stripos($item,'cr') !=== false)
echo 'yes';
else
echo 'no';
}
didn't test..
That's the kind of thing I was trying to get to work.
Doesn't work however

. Also, what is $item?