Echoing Array A Failure

Guys,

Why is not the array value getting echoed ?
It should echo:

Script 5 - Found Banned Word: rubbish

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned_words = array("trash", "rubbish");
var_dump($banned_words);

$content = "This is my rubbish text";
var_dump($content);

$banned = false;
var_dump($banned);

for($i=0; $i < count($banned); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
	   var_dump($banned_words[$i]);
       $banned = true;
	   var_dump($banned);
	   
	   echo "Script 5 - Found Banned Word:";
	   print_r($banned_words[$i]);
	   var_dump($banned_words[$i]);
       break;
    }
}

?>

RESULT:
I only see this on page:

array(2) { [0]=> string(5) “trash” [1]=> string(7) “rubbish” } string(23) “This is my rubbish text” bool(false)

Here is the less messy version:

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned_words = array("trash", "rubbish");
$content = "This is my rubbish text";
$banned = false;

for($i=0; $i < count($banned); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
       $banned = true;
	   echo "Script 5 - Found Banned Word:";
	   print_r($banned_words[$i]);
       break;
    }
}

?>

RESULT: I see a complete blank white page!
What do you think is wrong ?

Because you are doing count on the wrong variable. You did it on $banned instead of $banned_words

FYI, it will still fail after fixing that. strrpos is not the method you are looking for.

1 Like

Thanks. But strrpos is working fine now after I fixed the code according to your sugegstion. Look:

Debugging Version

<?php
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
$banned_words = array("trash", "rubbish");
$content = "This is my rubbish text";
$banned = false;
for($i=0; $i < count($banned); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
       $banned = true;
	   echo "Script 5 - Found Banned Word:";
	   print_r($banned_words[$i]);
       break;
    }
}
?>

I am getting this now:
array(2) { [0]=> string(5) “trash” [1]=> string(7) “rubbish” } string(23) “This is my rubbish text” bool(false) string(7) “rubbish” bool(true) Script 5 - Found Banned Word:rubbishstring(7) “rubbish”

Tidy Version

<?php
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned_words = array("trash", "rubbish");
$content = "This is my rubbish text";
$banned = false;

for($i=0; $i < count($banned_words); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
       $banned = true;
	   echo "Script 5 - Found Banned Word:";
	   print_r($banned_words[$i]);
       break;
    }
}
?>

Script 5 - Found Banned Word:rubbish

So, what function should I have used then according to you ? :slight_smile:
I just edited the codes I found here:

Check what they are using.

I’ll wait till you figure out the big that still exists. :smile:

Hint: add ‘ass’ to your banned words the put any of the following words in your content string: grass, asset, assassin

1 Like

I noticed at StackOverFlow they used strpos and stripos. Frankly, don’t know what the latter is but seems the same as the former. Have to check it out.
I know what you mean. It won’t look for exact match but fuzzy.
Meaning, if I want to match “ass” then it will find matches in these:
assassin
pass
passover
pass over

EDIT:
http://php.net/manual/en/function.strpos.php
http://php.net/manual/en/function.stripos.php

Seems like both are the same. Only difference is one ignores the case and the other does not.
Seems like latter better than former. However, still does not look perfect.

cpRadio,

Any idea why these 2 aren’t working. Complete blank pages. I don;t think I got the variables wrong on them:

<?php
//script 3: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned = array("evil", "devil");
//$email = $_POST['email'];
$email = "evil@devil.com";

foreach ($banned as $ban) {
    if (stripos($email,$ban) > -1){
        echo "Script 3 - Found Banned Word: $ban";
    }else{
        echo 'Email signed up!';
    }
}

?>
<br>



<?php
//script 4: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array

$content = "There are evil devils all over the place!";
$banned_words = array('evil', 'devil');

foreach($banned_words as $band) {
    $place = strpos($content, $band);
    if (!empty($place)) {
        echo "Script 4 - Found Banned Word: $band";
        exit;
    } else {
        echo "No banned words found!";
    }
}
?>
<br>

Both scripts work just fine. But still return false negatives.

1 Like

What I meant was that I don’t see the echos.
The echos should get displayed. They don’t on my side.

They did for me.

Well, then they are not the same if you’ve found a difference, are they? Sometimes you want to search case-sensitive, sometimes you don’t. So there’s a function for each because it saves you folding the haystack and needle to uppercase or lowercase yourself.

1 Like

Right-click and view source, is there anything there? It’s not something basic like running them from Windows Explorer rather than via the server? If you add an echo at the start of the php that isn’t inside a conditional, does that display?

Did you forget about your yesterday’s reply on how to solve blank pages?

1 Like

Even bearing in mind what you said, I still stick to my belief that:
“Seems like both are the same. Only difference is one ignores the case and the other does not.”
My statement is not untrue is it ? I said they “seem the same” and not “they are the same”. And something that seems or looks or feels the same but is not is “similar”. Similar but not the same.
The 2 concerned functions are similar. Not same. :wink:
And, I already figured-out why and where both differ. It is as you said, they save us a lot of time when in one hand dealing with case sensitive issue and on the other hand dealing with non-case sensitive issue. :slight_smile:

In another parallel universe, maybe. In this one, yes you’re wrong. Shock horror!

cpRadio & John_Betong,

For some reason the blank pages I see no more but the echos today. And this only after I had to refresh chrome about 10 times nearly each time of updating the scripts! Maybe a chrome bug.
I was gonna upload a clip for you guys to view how many times I have to click the refresh button for chrome to show the updates. No wonder I was seeing blank pages yesterday!
Mmm. I am now suspecting that a lot of problems I encounter is not due to my code writing but due to chrome messing about! Have to use fire fox on the side from now.

How am I wrong ? They are similar. One takes case into account and the other does not. That means the functions are similar. Not same.
So, we don’t need another parallel universe now do we ?
Speaking of parallel universe, I might aswell see if I can come-up with a php game called “parallel universe”. Or better, a “parallel function”! Lol!
Would you like to play “parallel gandalf” ? Gandalf the Grey and Gandal the White. Where both enter the same parallel and try killing each other. Lol!
Something like this:
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwirp9j7hsHVAhWBuI8KHeqABEMQFgglMAA&url=http%3A%2F%2Fwww.imdb.com%2Ftitle%2Ftt0267804%2F&usg=AFQjCNF-JJUko6j1n4bSIQVftn3zGncpdA

Try adding the time:

echo '<br>' .date('H:i:s');
1 Like

Ok, I added your suggesions.
Thanks.

But on this script, why is not the print_r echoing the matched banned word ?

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

/*
Formula:
returns true if $needle is a substring of $haystack
function contains($needle, $haystack)
{
    return strpos($haystack, $needle) !== false;
}
*/

//script 1: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array
//http://php.net/manual/en/function.in-array.php
//Simplest check.

$content = 'Evil & devillish they were!';
$targets = array("evil", "devil");

if (in_array($content, $targets)) {
    print_r($targets[]);
	echo '<br>' .date('H:i:s');
}
?>
<br>

Don’t tell me, I have to use the incrementer to reference the number of the array, like I did here:

<?php

//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned_words = array("trash", "rubbish");
$content = "This is my rubbish text";
$banned = false;

for($i=0; $i < count($banned_words); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
       $banned = true;
	   echo "Script 5 - Found Banned Word:";
	   print_r($banned_words[$i]);
       break;
    }
}
?>
<br>

I thought it generally checks the whole array rather than get the script to loop and check each array item one by one for matches.

Similar question goes for this one too:

<?php
//script 6: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

//$email = $_POST['email'];
$email = "evil@devil.com";

if(preg_match("/\b(evil|devil)\b/", $email)){
    echo "Script 6 - Found Banned Word: HOW DO I DISPLAY THE FOUND BANNED WORD HERE ? WOULD LIKE TO SEE SNIPPET EXAMPLE";
}

?>

May I suggest that once you manage to create some script that does a unique task then create a function in a functionslibrary.php. Error_reporting and ini_set display_errors could also be part of the library.

test-001.php

<?php
declare( strict_types=1 );
require 'functionslibrary.php';

//

I already did that. That way, my many paged scripts did not have the error codes repeated on every page. But when I copy paste each of my scripts/pages codes to this forum then you guys complain where my error reporting codes are. hence, I added them on every page now. I will take them out once the project comes to an end (edited and final version). So, don’t worry.
As of now, the error reporting codes would stay on every page so when I copy-paste their codes to this forum then others (who don’t know any of this) don’t start screaming why the error reporting codes aren’t present on the script/page. Understand ?
Now, how-about I start getting answers on my previous post ? :wink:

Look what my config.php looks like. It is part of my member registration-login script (check my thread on it):

<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// session start
if(!session_start()) {
	session_start();
}

// include files
include 'conn.php';
include 'site_details.php';

// include functions
include 'functions.php';

?>