Hi,
I am a novice at this.
I am trying to find an instance of a string within another string. I have found several functions that do this. I have tested the functions and they seem to work ok, so I know the syntax is correct.
However, I am passing a variable containing one string to this function in another file. The string is being passed ok, but the function will not correctly tell me that one string contains another when I know that it does.
For example.
<?php
$pos = stristr("<?=$cat?>", "<?=$choosecat?>");
if ($pos === false)
echo "The string '$choosecat' was not found in the string '$cat'";
else {
echo "The string '$choosecat' was found in the string '$cat'";
echo " and exists at position $pos";
}
?>
always returns false.
Can anyone give me a clue why this isn't working please?
Thanks
Help With Strings
Moderator: General Moderators
Re: Help With Strings
Well first of all strpos() is the string function that returns string position, second of all have you checked that the variable does contain the said string but, hmm lets say... ( and this might be a totally crazy idea ) but outputting the variable before you call the function to see if it contains the string?
I would expect it to return false for those hard coded values, since neither one of those strings contains the other.
I would expect it to return false for those hard coded values, since neither one of those strings contains the other.
Re: Help With Strings
Try:profitweaver wrote:$pos = stristr("<?=$cat?>", "<?=$choosecat?>");
Code: Select all
$pos = stristr(" . <?php echo $cat; ?> . ", " . <?php echo $choosecat; ?> . ");Re: Help With Strings
I'm sorry I don't see the logic, the haystack does not contain the needlecalifdon wrote:Try:profitweaver wrote:$pos = stristr("<?=$cat?>", "<?=$choosecat?>");Code: Select all
$pos = stristr(" . <?php echo $cat; ?> . ", " . <?php echo $choosecat; ?> . ");
Re: Help With Strings
To be sure. My bad.
Even this contains assumptions about the values of those variables, but should give the OP some idea of how to use string search. I hope.
Code: Select all
<?php
$pos = stristr("$cat", "$choosecat");
?>Re: Help With Strings
hmm, forget about variable expanding also, I thought those were single quotes.. my bad as well, yes you would need to double check the value of those variables as well, unless you're trying to use the variable names literally..
Anyways you still want strpos()
Anyways you still want strpos()
-
profitweaver
- Forum Newbie
- Posts: 2
- Joined: Sun Oct 05, 2008 2:04 am
Re: Help With Strings
Thanks for your efforts people.
I have discovered that
$pos = stristr($cat,$choosecat);
Does what I need. I was over complicating matters.
I have discovered that
$pos = stristr($cat,$choosecat);
Does what I need. I was over complicating matters.