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

Edited by james909
Link to comment
Share on other sites

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

}

}

?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 by james909
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.