Jump to content

php echo wildcards


laurajohn89

Recommended Posts

Hi.  I am just wondering can you use some kind of wildcards in an echo statement.  I want to search my database and find for instance anything with an and in it. This is what I have tried:

 

<?php if($type=='*and*'){ echo "checked=\"true\""; }?>

 

Obviously the *'s don't work but is there any kind of way I can search for anything with and in it.

 

BTW the above statement works if I remove the *'s and just want to search for and on its own.

 

Thanks for your help :)

Link to comment
https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/
Share on other sites

if you cant use the query, then per teddy's suggestion

if($pos !== false){ echo "checked=\"true\""; }

 

you would think === would also work, but it doesn't. Because $pos will return a number when true (like 3, or 4) when using === it fails because the number 4 is not identical to the boolean false. it is convertible to it, but === (identical) means that the right hand side and left hand side or both equal in value and the same type

Link to comment
https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036136
Share on other sites

Where is $type defined? It might be a problem going else where. Can you echo $type?

 

<?php
echo '$type';
?>

Does it give you the right value?

If so.. try out (Else where on your form.. so your basically troubleshooting)

<?php
$pos = stripos($type,'and'); //Going by mikesta07
if($pos === true){ echo "correct"; } else { echo 'Something was wrong'; exit; }
?>

Link to comment
https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036150
Share on other sites

Try this.. It might be failing because both $type and 'and' are both the same.

<?php
$type = 's'.$type; //Adding on a letter before the actual word and
$pos = stripos($type,'and'); //Going by mikesta07
if($pos === true){ echo "correct"; } else { echo 'Something was wrong'; exit; }
?>

Link to comment
https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036171
Share on other sites

($pos === true)

 

as i said before, that doesn't work.

($pos !== false)

 

will though (verified this through testing)

 

using regex would work too, but since you are only checking for the word and to be in a string, using regex seems unecessary. However, if your only looking for the word "and", then oni-kuns regex would work, although theoretically, changing the needle from

"and" to " and " (just padding it with spaces) would work.

$pos = stripos($type,' and ');

 

but that wouldn't match the word and before a period (like "blaah blah and. blah blah") In that case regex would be useful

Link to comment
https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036237
Share on other sites

<td width="25%"><p class="contentclass"><input type="checkbox" name="type[]" id="type[dressage]" value="Dressage" <?php $pos = stripos($type,' Dressage '); if($pos !== false){ echo "checked=\"true\""; }?>/>Dressage</p></td> 

 

(replaced and with Dressage as this is what I am looking for)

 

This neither ticks the box when $type = dressage or when $type = Dressage,Ride etc

Link to comment
https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036375
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.