Part of a string.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Part of a string.

Post 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 ?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Part of a string.

Post by VladSun »

substr()
array_filter()
array_walk()
There are 10 types of people in this world, those who understand binary and those who don't
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: Part of a string.

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Part of a string.

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: Part of a string.

Post 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
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: Part of a string.

Post 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?
Last edited by Suudsu on Sun Nov 23, 2008 7:20 pm, edited 2 times in total.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Part of a string.

Post 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..
Suudsu
Forum Newbie
Posts: 9
Joined: Sun Nov 23, 2008 2:55 pm

Re: Part of a string.

Post 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?
Post Reply