james909 Posted February 20, 2013 Share Posted February 20, 2013 what is the PHP wildcard character in an array <?php $banned = array('User:%','User_talk:%'); $allowed = array('edit','history'); if (in_array($_GET['title'], $banned)) { // do this } else { // do nothing } ?> the title's from $_GET look like this: title=User:randomname title=User:example.com title=User:another-name title=User:user-name.admin i have tried using the following symbols: <?php $banned = array('User:$','User_talk:$'); <?php $banned = array('User:%','User_talk:%'); <?php $banned = array('User:*','User_talk:*'); none seem to be working Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 20, 2013 Share Posted February 20, 2013 Probably because there is no such thing as a wildcard character when using in_array(). Either build your own function to do a search using a wildcard character of your choosing - or put the values into a database. Quote Link to comment Share on other sites More sharing options...
james909 Posted February 20, 2013 Author Share Posted February 20, 2013 (edited) thank you psycho, i added the following line to the code, to assign the * character as a wildcard <?php $one = $_REQUEST["term"] . "*"; $banned = array('User:%','User_talk:%'); $allowed = array('edit','history'); if (in_array($_GET['title'], $banned)) { // do this } else { // do nothing } ?> and it is still not picking up on any of the User: in the array and it is still not pickin Edited February 20, 2013 by james909 Quote Link to comment Share on other sites More sharing options...
james909 Posted February 20, 2013 Author Share Posted February 20, 2013 how do i build my own function? the line i added $one = $_REQUEST["term"] . "*"; does not seem to be assigning the * character as a wildcard symbol thanks james Quote Link to comment Share on other sites More sharing options...
james909 Posted February 20, 2013 Author Share Posted February 20, 2013 so know i am trying the preg_grep like so: <?php $banned = array('User:%','User_talk:%'); $banneduser = preg_grep("/User(\w+)/", $_GET); if (in_array($_GET['title'], $banned)) { // do this } else { if (in_array($_GET['title'], $banneduser)) { // do this } else { // do nothing } } ?> Quote Link to comment Share on other sites More sharing options...
kicken Posted February 20, 2013 Share Posted February 20, 2013 You can't use in_array at all. That function does not support any sort of wildcard matching. What you need to do is use a foreach loop on the array to test each element individually using something like preg_match. Then you can wrap that code into your own function for easy re-use. Such as: function fuzzy_in_array($regex, $array){ foreach ($array as $item){ if (preg_match($regex, $item)){ return true; } } return false; } Quote Link to comment Share on other sites More sharing options...
james909 Posted February 20, 2013 Author Share Posted February 20, 2013 (edited) thank you for your reply kicken, i dont use php programming very often and cant see me using it much in the future it is just to do this one task of getting the facebook comments box to appear on some of my website pages and not other. so i changed the coding using kickens advise, and this is what i made: <?php $banned = array('Main_Page','Community_portal','Current_events','Special:RecentChanges','Help:Contents'); function user_check($regex, $array){ foreach ($array as $item){ if (preg_grep("/User(\w+)/", $item)){ return true; } } return false; } if (in_array($_GET['title'], $banned)) { // do this nothing } else { if ( user_check == false ) { // load facebook comment box } } ?> now, no page is loading the facebook comment box, so i am assuming my preg_grep coding or checking if the function is true or false coding is in-correct? Edited February 20, 2013 by james909 Quote Link to comment 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.