laurajohn89 Posted April 2, 2010 Share Posted April 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/ Share on other sites More sharing options...
mikesta707 Posted April 2, 2010 Share Posted April 2, 2010 in your query itself you can use wildcards in combination with the LIKE operator. for example ] $q = "SELECT * FROM Table WHERE column LIKE '%search term%'"; will match any row where column has "search term" in it. the % act as wild cards. Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036115 Share on other sites More sharing options...
TeddyKiller Posted April 2, 2010 Share Posted April 2, 2010 Or you can try this <?php $pos = strpos($type,'and'); if($pos === true){ echo "checked=\"true\""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036117 Share on other sites More sharing options...
laurajohn89 Posted April 2, 2010 Author Share Posted April 2, 2010 Thanks but it doesn't work - leaves it blank. I can't but it in the SQL statement as I search for many of these in one go. Thanks for the help though Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036120 Share on other sites More sharing options...
mikesta707 Posted April 2, 2010 Share Posted April 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036136 Share on other sites More sharing options...
laurajohn89 Posted April 2, 2010 Author Share Posted April 2, 2010 Still no luck. I swear I am cursed. It doesn't work whether and is on its own or amongst other letters. Thanks once again though. I must be able to find a way...all nighter I think. Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036145 Share on other sites More sharing options...
mikesta707 Posted April 2, 2010 Share Posted April 2, 2010 the following <?php $type = "stuff with and in it"; $pos = strpos($type,'and'); if($pos !== false){ echo "checked=\"true\""; } ?> works perfectly for me. try stripos() instead of strpos if you want to make the search case insensitive Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036149 Share on other sites More sharing options...
TeddyKiller Posted April 2, 2010 Share Posted April 2, 2010 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036150 Share on other sites More sharing options...
laurajohn89 Posted April 2, 2010 Author Share Posted April 2, 2010 <?php echo $type ?> returns: and <?php $pos = stripos($type,'and'); //Going by mikesta07 if($pos === true){ echo "correct"; } else { echo 'Something was wrong'; exit; } ?> returns: something was wrong Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036166 Share on other sites More sharing options...
TeddyKiller Posted April 2, 2010 Share Posted April 2, 2010 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036171 Share on other sites More sharing options...
laurajohn89 Posted April 2, 2010 Author Share Posted April 2, 2010 same error again.. sorry this may be frustrating Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036173 Share on other sites More sharing options...
trq Posted April 2, 2010 Share Posted April 2, 2010 Your looking for preg_match. Nothing at all to do with echo. Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036175 Share on other sites More sharing options...
jcbones Posted April 2, 2010 Share Posted April 2, 2010 <?php $column_to_search = 'column'; $search = $_POST['search']; $ex = explode(' ',$search); foreach($ex as $value) { $str[] = "column LIKE '%$value'"; } $search = implode(' || ',$str); $q = "SELECT * FROM Table WHERE $search"; Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036180 Share on other sites More sharing options...
oni-kun Posted April 2, 2010 Share Posted April 2, 2010 Meh, preg_match on your first code statement would look like this: <?php if(preg_match('/[ *]and[ *]/i', $type)) { echo 'checked="true"'; } ?> Which would match " and " but not "random" or "Anderson" Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036187 Share on other sites More sharing options...
mikesta707 Posted April 3, 2010 Share Posted April 3, 2010 ($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 Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036237 Share on other sites More sharing options...
laurajohn89 Posted April 3, 2010 Author Share Posted April 3, 2010 <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 Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036375 Share on other sites More sharing options...
laurajohn89 Posted April 3, 2010 Author Share Posted April 3, 2010 works now Thanks fot the help. The above was true except I didnt need the spaces either side of the word. Quote Link to comment https://forums.phpfreaks.com/topic/197400-php-echo-wildcards/#findComment-1036378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.