Jump to content

How To Echo Array Value ?


phpsane

Recommended Posts

Hi,

 

The following code seems to be echoing the array numbers. I actually need to echo the array value and not the numbers.

Echo: Values of the matched banned words.

Echo: Values of the non-matched banned words.

So, how to correct the following code which shows results like this:

 

Script 5a - No Match: (0)
Script 5a - Banned: array(1)
Script 5a - Banned: array(2)
Script 5a - Banned: array(3)
Script 5a - Banned: array(4)

 

NOTE: "0" is being shown as "no match". But how come when "0" exists both in the content and banned words array ?

<?php
//script 5d: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
 
$banned_words = array("0","1","2","3","4");
$content = "0,1,2,3,4,5,6,7,8,9";
for($i=0; $i < count($banned_words); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
        echo "Script 5a - Banned: array($banned_words[$i])<br>";
       // Place "break;" if you want it to stop after finding a match.
}else{
        echo "Script 5a - No Match: ($banned_words[$i])<br>";
    }   // Why is it echoing the array numbers instead of the array values ?
}
 
?>
Link to comment
Share on other sites

Nor this works like I want:

 

 

<?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);
 
 
//script 5b: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string
 
$banned_words = array("0","1","2","3","4");
$content = "0,1,2,3,4,5,6,7,8,9";
$banned = false;
 
for($i=0; $i < count($banned_words); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
       $banned_words = true;
  echo "Script 5b - Banned:<br>";
  print_r($banned_words);
       // Place "break;" if you want it to stop after finding a match.
}else{
        echo "Script 5a - No Match: $ban<br>";
print_r($banned_words);
    }   // Why I get error that this closing bracket is unexpected when it is the closing bracket for the ELSE ?
}
 
?>
Link to comment
Share on other sites

"0" is being shown as "no match". But how come when "0" exists both in the content and banned words array ?

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

http://php.net/manual/en/function.strrpos.php

So, you need to use !== instead of !=.

 

Why is it echoing the array numbers instead of the array values ?

Maybe because array values are array numbers?

 

Why I get error that this closing bracket is unexpected when it is the closing bracket for the ELSE ?

I don't get that error by I do get:

Notice: Undefined variable: ban in D:\wamp64\www\test.php on line 26

Link to comment
Share on other sites

You're assigning number values to the array indices. So, the script is printing the array values - they just happen to be numbers. Change

$banned_words = array("0","1","2","3","4");

to

$banned_words = array("PBR","quinoa","kale","portland","beard");

Then head over to https://hipsum.co/ and grab some sample for $content.

Link to comment
Share on other sites

So, you need to use !== instead of !=.

 

Maybe because array values are array numbers?

 

I don't get that error by I do get:

Notice: Undefined variable: ban in D:\wamp64\www\test.php on line 26

 

Thanks for pointing out the: !==.
Yes, I figured what I was doing wrong, after I made that post. I was getting numbers as values because the array values were numbers. Silly me!
Link to comment
Share on other sites

You're assigning number values to the array indices. So, the script is printing the array values - they just happen to be numbers. Change

$banned_words = array("0","1","2","3","4");

to

$banned_words = array("PBR","quinoa","kale","portland","beard");

Then head over to https://hipsum.co/ and grab some sample for $content.

 

 

Is this your signature: Then head over to https://hipsum.co/ and grab some sample for $content.

I did not realize it first and headed over to the link and got puzzled what it was about. That was a clever tactic on your part to make your post readers click the link. ;)

If you're into finding tactics to generate clicks then I'm in the same boat. Keep in touch and PM me whenever you come-up with any manual fraudulent-free click generating tactics. I might attempt to build a php script and a .exe bot around it. We can start a joint venture. :)

 

Again, yes. I figured what I was doing wrong, after I made that post. I was getting numbers as values because the array values were numbers. Silly me!

Link to comment
Share on other sites

Is this your signature: Then head over to https://hipsum.co/ and grab some sample for $content.

No, it is not. Signatures are separated and have lighter-color text (see mine below).

 

They were simply suggesting that instead of what you're currently doing, you should grab a paragraph or two of random text and use that as your $content value. You could then pick a few words that are in or not in those paragraphs to use in your $banned_words array. Perhaps then you wouldn't get confused by all the numbers as you'd have distinct words to look at.

Link to comment
Share on other sites

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.