Jump to content

PHP wildcard symbol in array


james909

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/274722-php-wildcard-symbol-in-array/
Share on other sites

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 :confused:

 

 

and it is still not pickin

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

}

}

?>

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;
}

 

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?

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.