Just after some help, I have an array with disallowed words in which I cant show here as they are obscene.
What I want to do is check if $_POST['urlprefix'] != one of the array elements, I am wondering what the easiest way to do this is. Obviously the below only stops one element of the array.
Thanks for any help you may provide
if($_POST['urlprefix'] != $arr[1]) {
print("You've Passed");
} else {
print("You Are Not Allowed To Use That URL Prefix");
}
The examples with in_array() are a quick way to achieve that. You can also use a case-insensitive in_array function by using preg_grep.
But, if ship is a bad word, do you want to allow shippy ? The following code will forbid any prefixes that contain the word, too.
$disallow = false;
foreach($arr as $bad_word) {
if(stripos($_POST['urlprefix'], $bad_word) !== false) {
$disallow = true;
break;
}
}
if($disallow) {
die('Bad words in URL!');
}
You can use the in_array function :
if (!in_array($_POST['urlprefix'], $arr))
{
print("You've Passed");
}
else
{
print("You Are Not Allowed To Use That URL Prefix");
}
http://www.php.net/manual/fr/function.in-array.php
You can use this way. in_array is slower than this method. See more about array_flip.
$words = array('damn','shit');
$flip = array_flip($words);
if(!isset($flip[$_POST['urlprefix']])){
//valid url
}
You can use php in_array function. It checks if a value exists in an array
if(!in_array($_POST['urlprefix'], $arr)) {
print("You've Passed");
} else {
print("You Are Not Allowed To Use That URL Prefix");
}
Related
I need one help. I need to check that string is present inside array or not and also it should search letter wise using PHP. I am explaining my code below.
$resultArr=array("9937229853","9937229856","9937229875");
$searchValue="+919937229853";
Here I need to check that some of the value from $searchValue is present inside in array or not. I am doing like below but its not giving me the proper result.
$searchValue="+919937229853";
$resultArr=array("9937229853","9937229856","9937229875");
if(!in_array($searchValue, $resultArr))
{
$flag=1;
}else{
$flag=0;
}
echo $flag;
As per my requirement here result should print 1 because some value from $searchValue also present in that array but the echo result is coming 0.Please help me.
$searchValue="+919937229853";
$resultArr=array("9937229853","9937229856","9937229875");
foreach($resultArr as $value)
{
if(strpos($value,$searchValue) || strpos($searchValue,$value) || $searchValue==$value)
{
$flag = 1;
break;
}
else
$flag=0;
}
echo $flag;
I think this will do what you are looking for. in_array() method search string in array but for the exact string. strpos can search substring in long string and return the offset number or the index of substring in the long string.
you can try code like below.
if(!in_array(substr($searchValue,-10), $resultArr))
$searchValue="+919937229853";
$searchValue = str_replace("+91","",$searchValue);
$resultArr=array("9937229853","9937229856","9937229875");
if(in_array($searchValue, $resultArr))
{
$flag=1;
}else{
$flag=0;
}
echo $flag;
User str_replace function replace first three charater from string
$flag=0;
for($i=0;$i<strcmp($searchValue);$i++){
for($j=0;$j<strcmp($searchValue);$j++){
$part=substr($searchValue,$i,$j)
array_filter($resultArr, function($el) use ($part) {
if ( strpos($el, $part) !== false ){
$flag=1;
}
});
}
}
The function below will return true if either
your $searchValue is in the array (in_array), or
if any item of the array is a substring of $searchValue
in other words: If part of $searchValue is in the array.
This is the code and how you call it:
function search($needle, $haystack) {
// is $needle contained in the array as it is?
if (in_array($needle, $haystack))
return true;
// Is any part of $needle part of the array?
foreach ($haystack as $value) {
if (strpos($needle, $value) !== FALSE)
return true;
}
return false;
}
$resultArr = array("9937229853", "9937229856", "9937229875");
$searchValue = "+919937229853";
$result = search($searchValue, $resultArr);
var_dump($result);
I am trying to use the following code to check if the current URL is within an array.
$reactfulPages = array(
'url-one',
'url-two',
'url-three',
);
if (strpos($url, $reactfulPages) == true) {
echo "URL is inside list";
}
I think the way I have set up the array is incorrect as the following code (checking for one URL) works fine..
if (strpos($url,'url-one') == true) { // Check if URL contains "landing-page"
}
Can anyone help me out?
The array is fine, the functions to check is not the right way. The strpos() function is for checking the string position(s).
The right way to check if something is in your array you can use the in_array() function.
<?php
$reactfulPages = array(
'url-one',
'url-two',
'url-three',
);
if(in_array($url, $reactfulPages)) {
echo "The URL is in the array!";
// Continue
}else{
echo "The URL doesn't exists in the array.";
}
?>
I hope this will work for you.
The function strpos() looks for a substring within a string, and returns the the position of the substring if it is found. That is why your last example works.
If you want to check whether something exists in an array, you should use the in_array() function, like so:
$reactfulPages = array(
'url-one',
'url-two',
'url-three',
);
if (in_array($url, $reactfulPages) == true) {
echo "URL is inside list";
}
However, since you're comparing URL's, I'm assuming you want to check whether the URL contains one of the strings from the array, not necessarily matching them as a whole.
In this case, you will need to write your own function, which could look like this:
function contains_any($string, $substrings) {
foreach ($substrings as $match) {
if (strpos($string, $match) >= 0) {
// A match has been found, return true
return true;
}
}
// No match has been found, return false
return false;
}
You can then apply this function to your example:
$reactfulPages = array(
'url-one',
'url-two',
'url-three',
);
if (contains_any($url, $reactfulPages)) {
echo "URL is inside list";
}
Hope this helps.
I'm trying to check if the following is empty or not.
{"players":""}
I have a function that gets that from an api/site and.. well, heres the code.
function getPlayers($server) {
// Fetches content from url and json_decodes it
$playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
// Attempting to check if it's empty.
if ($playersList != "") {
// convert list of comma-separated names into array
$players = explode(',', $playersList->players);
foreach ($players as $player) {
echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
}
} else {
return 'empty';
}
}
However, using !=, empty(), or isset(), I still get an empty string, example:
https://minotar.net/avatar//32
Where it should be..
https://minotar.net/avatar/Notch/32
When it's empty, I'd like it to just return 'empty'.
I'm not sure what I'm doing wrong. Any ideas?
In pure php you can check the url segments like
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);
if($segments[2] == '') {
}
//or
if(empty($segments[2])) {
}
//or do your condition
if you are using codeigniter you might say
if(empty($this->uri->segment(2)))
But be sure you loaded the url helper
Hope I understand your question!
Since you were able to have some output, see my changes in the codes.
function getPlayers($server) {
// Fetches content from url and json_decodes it
$playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
// Attempting to check if it's empty.
if ($playersList != "") {
// convert list of comma-separated names into array
$players = explode(',', $playersList->players);
// check conversion
if(is_array($players) && !empty($players){
foreach ($players as $player) {
echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
}
} else {
return 'empty';
}
} else {
return 'empty';
}
}
You should do this;
print_r($playersList);
just after you set it to see what it actually is. My guess is that you are not getting what you suspect from the getURL call.
Add one more equals sign to take type comparison into account as well
if ($playerList !== '')
try this
if (isset($playersList) && is_array($playersList) && !empty($playersList)) {
// convert list of comma-separated names into array
$players = explode(',', $playersList->players);
foreach ($players as $player) {
echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
}
} else {
return 'empty';
}
<?php
$check = array ("85.49.","85.62.");
foreach($check as $var) {
if (ereg($var, $_SERVER['REMOTE_ADDR'])) {
$intruder = 0;
}
else {
$intruder = 1;
}
if ($intruder = 0);
echo 'bugger off';
}
else{
echo 'welcome';
}
?>
What could be wrong with my second else? Dreamweaver red flags it and my server says error. All I want to do is get it to behave one way or another if $intruder is 1 or 0.
if ($intruder = 0);
First, you have an extra semicolon, so the body of the if statement is empty.
Secondly, = is assignment, you need comparison: ==.
The structure is not clear, but probably the next problem is your loop, which will overwrite $intruder in each iteration, so at the end it will contain the result of the last comparison.
The problem you ask about is on this line:
if ($intruder = 0);
You should be using == to compare the values, not = which is for assignment. And you should have a curly brace { after that instead of a semicolon.
Also, all of the ereg* functions are deprecated and should not be used. They will eventually be removed from the language entirely. To check if a word contains another word, just use strpos.
Your logic is also wrong for what you appear to be doing. You need to not overwrite the values of $intruder in each iteration of the loop. Set it to 0 before the loop, set it to 1 if there's a match in the loop, then after the loop is complete you'll know if there was a match during any of the comparisons and can print the appropriate message.
$found = 0;
foreach ($check as $var) {
if (strpos($_SERVER['REMOTE_ADDR'], $var) === 0) {
$found= 1;
}
}
if ($found == 1) {
echo "You are in my list.";
} else {
echo "You are not in my list.";
}
?>
Change this:
if ($intruder = 0);
To this:
if ($intruder == 0) {
First error:
if ($intruder = 0);
fix it in this way:
if ($intruder == 0)
echo 'bugger off';
else
echo 'welcome';
Second error:
The use of ereg is deprecated. Replace it with preg_match(); http://www.php.net/manual/en/function.preg-match.php
Friend, please do not be offended, but I'd rather have asked "is there anything right in this code?"
<?php
$check = array ("85.49.","85.62.");
foreach($check as $var) {
// Here you use the deprecated ereg instead of preg_match or, better, strpos
// However, the regular expressions would be wrong - what if 192.85.49.3 comes by?
if (ereg($var, $_SERVER['REMOTE_ADDR'])) {
$intruder = 0;
}
else {
$intruder = 1;
}
// Here you do not close the foreach, so that the following code gets executed
// repeatedly
// Here you place a ; after the if, so the if body is empty and bugger off gets
// triggered always.
// Which changes little, since $intruder = 0 is an assignment (use == instead)
// (see note)
if ($intruder = 0);
echo 'bugger off';
}
// Anyway, logically "$intruder == 0" means "NOT an intruder", so you are actually
// telling friends to bugger off and welcome intruders :-)
else{
echo 'welcome';
}
?>
Note: it is maybe voodoo programming (I found it on Maguire's 'Writing Solid Code'), but I think you might get into the habit of checking values the other way:
if (0 == $intruder)
This way if you ever drop a = again, it will not create a new statement doing something you wouldn't want, but it will become a syntax error that makes itself immediately visible.
Anyway, the code for what you want should be:
<?php
$check = array ("85.49.","85.62.");
$matches = false;
foreach($check as $var)
{
if (0 === strpos($_SERVER['REMOTE_ADDR'], $var))
{
$matches = true;
// There is one match, no sense in checking further
break;
}
}
if ($matches)
{
// He is in our little list - tell him something
print "You match.";
}
?>
Simple yaar.Just edit:
if ($intruder == 0);
echo 'bugger off';
}
else{
echo 'welcome';
}
I need to search a string for any occurances of another string in PHP. I have some code that I've been playing with, but it doesn't seem to be working.
Here is my code:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos == false) {
echo "allow";
exit;
} else {
echo "deny";
exit;
}
}
I have tried some of the options below, but it still does not find the match. Here is what I'm searching:
I need to find:*
blah
In:
http://blah.com
Nothing seems to find it. The code works in regular sentences:
Today, the weather was very nice.
It will find any word from the sentence, but when it is all together (in a URL) it can't seem to find it.
When checking for boolean FALSE in php, you need to use the === operator. Otherwise, when a string match is found at the 0 index position of a string, your if condition will incorrectly evaluate to true. This is mentioned explicitly in a big red box in the php docs for strpos().
Also, based on a comment you left under another answer, it appears as though you need to remove the exit statement from the block that allows access.
Putting it all together, I imagine your code should look like this:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos === false) { // use === instead of ==
echo "allow";
} else {
echo "deny";
exit;
}
}
Update:
With the new information you've provided, I've rewritten the logic:
function isAllowed($location) {
$keywords = array('blah', 'another-restricted-word', 'yet-another-restricted-word');
foreach($keywords as $keyword) {
if (strpos($location, $keyword) !== FALSE) {
return false;
}
}
return true;
}
$location = 'http://blah.com/';
echo isAllowed($location) ? 'allow' : 'deny';