Compairing html string to hex string with php - php

I am trying to compare two strings. When compared they are unequal to the computer. But to the human eye the strings appear to be the same.
When I run a test to check the bin2hex with php they are clearly unequal. Some how a set of double quotes is be read as html. Here are some examples:
$string1 = strlen(html_entity_decode($_SESSION['pageTitleArray'][$b]));
$string2 = strlen(html_entity_decode($value));
echo 'Checking: ' . $_SESSION['pageTitleArray'][$b] . " " . bin2hex($_SESSION['pageTitleArray'][$b]);
echo '<br>';
echo 'testing ' . $string1 . " = "bin2hex($string1) . " " . bin2hex($string2);
echo '<br>';
echo 'With: ' . $value . " " . bin2hex($value);
The code above will out put the following information.
Checking: 1 PAIR OF BBC HEAD GASKET GASKETS MULTI LAYERED STEEL 4.585"
312050414952204f46204242432048454144204741534b4554204741534b455453204d554c5449204c41594552454420535445454c20342e35383522
testing 3630 3632 With: 1 PAIR OF BBC HEAD GASKET GASKETS MULTI
LAYERED STEEL 4.585″
312050414952204f46204242432048454144204741534b4554204741534b455453204d554c5449204c41594552454420535445454c20342e3538352623383234333b
false
I am kinda lost on what to do... Any help would be greatly appreciated. Below is the rest of the code so you can get a total feel for what I'm trying to accomplish.
for($b = 0; $b < count($_SESSION['pageTitleArray']); $b++)
{
foreach($_SESSION['pushIdArrayQuery'] as $key => $value)
{
$string1 = strlen(html_entity_decode($_SESSION['pageTitleArray'][$b]));
$string2 = strlen(html_entity_decode($value));
echo 'Checking: ' . $_SESSION['pageTitleArray'][$b] . " " . bin2hex($_SESSION['pageTitleArray'][$b]);
echo '<br>';
echo 'testing ' . $string1 . " = "bin2hex($string1) . " " . bin2hex($string2);
echo '<br>';
echo 'With: ' . $value . " " . bin2hex($value);
if(trim($_SESSION['pageTitleArray'][$b]) == trim($value))
{
echo '<br>';
echo '<h1>Success</h1>';
echo '<br>';
echo '<b>Key: </b>' . $key;
echo '<br>';
echo 'Page Id: ' . $_SESSION['pushTitleArrayQuery'][$key];
echo '<br> ';
}
else {
echo '<br>';
echo 'false';
echo '<br>';
}
}
}

You have two different types of quotations mark (" on the first string and ″ on the second one).
To human eyes they seems similar but they are complete different characters to PHP.

This is how I fixed the problem. Notice, I removed any html attributes with the html_entity_decode() function. Then, from there I was able to strip away all other special characters by using preg_replace() and compare the results.
//Loop through all the page titles from the directories
for($b = 0; $b < count($_SESSION['pageTitleArray']); $b++)
{
//Loop through the page titles gathered from the database
foreach($_SESSION['pushTitleArrayQuery'] as $key => $value)
{
//use html decode to remove the quotes with ENT_NOQUOTES
$removeHtml = html_entity_decode($value, ENT_NOQUOTES);
//Test to make sure the encoding are the same for both variables.
$detectEncoding = mb_detect_encoding($value, "auto");
//remove all non alphanumeric variables in the first string.
$string1 = preg_replace("/[^a-zA-Z0-9]/", "", $_SESSION['pageTitleArray'][$b]);
//remove all non alphanumeric variables in the second string.
$string2 = preg_replace("/[^a-zA-Z0-9]/", "", $removeHtml);
//Print out to see what the results look like from decode and encoding functions
echo 'Testing the removal of html quotes: ' . $removeHtml . ' Encoding: ' . $detectEncoding;
echo '<br>';
//Show what variables are being compaired.
echo 'Checking: ' . $string1 . " " . bin2hex($_SESSION['pageTitleArray'][$b]);
echo '<br>';
echo 'With: ' . $string2 . " " . bin2hex($value);
//If statement to check if the to strings match.
if(trim($string1) === trim($string2))
{
echo '<br>';
echo '<h1>Success</h1>';
echo '<br>';
echo '<b>Key: </b>' . $key;
echo '<br>';
echo 'Page Title: ' . $_SESSION['pushTitleArrayQuery'][$key];
echo '<br>';
echo 'Page Id: ' . $_SESSION['pushIdArrayQuery'][$key];
echo '<br> ';
}
else {
echo '<br>';
echo 'false';
echo '<br>';
}
echo '<br>';
}
}

Related

How to properly get a return value from a PHP function?

The code snippet below generates '12290' from the second echo output but the third one is still zero.
How do I get a proper return value from mytest() function?
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
mytest(12290);
echo 'third: ' . $testId . '<br>';
You forgot to assign the return value to the variable again.
It should be this:
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
$testId = mytest(12290);
echo 'third: ' . $testId . '<br>';
Instead of:
mytest(12290);
echo 'third: ' . $testId . '<br>';
Try:
echo 'third: ' . mytest(12290) . '<br>';

php wont let me echo JSON string

Throught an AJAX call I want to pick some data from DB and send back to client.
$result is return value of SELECT QUERY
Case1:This works fine
$str = "[";
while($row = mysqli_fetch_assoc($result)) {
$str = $str ."{".
'"Name":'. '"'. $row["Name"] . '"'
. "},";
}
}
$str = $str . "]";
echo str;
CASE2:This works fine too:
$str = "[";
while($row = mysqli_fetch_assoc($result)) {
$str = $str ."{".
'"ID":'. '"'. $row["ID"] //SEE HERE
. "},";
}
} else {
echo "0 results";
}
$str = $str . "]";
echo $str;
CASE3: BUT FOLLOWING GIVES error with 500()
$str = "[";
while($row = mysqli_fetch_assoc($result)) {
$str = $str ."{".
'"ID":'. '"'. $row["ID"] . '",' //SEE HERE
'"Name":'. '"'. $row["Name"] . '"'
. "},";
}
} else {
echo "0 results";
}
$str = $str . "]";
echo $str;
I want to send echo str after making it in JSON format and parse it at client side using `JSON.parse(). Case1 and 2 work fine when I have only one key-value pair. But as soon as I put 2 key-value pairs I get error:
POST https://******************.php 500 ()
You really need to use the appropriate functions.
Get into good programming habits.
$rtn = []; //create array
while($row = mysqli_fetch_assoc($result)){
//push result into array
$rtn[] = ['ID'=> $row["ID"], 'Name'=> $row['Name']];
}
if($rtn)//if array has entries
echo json_encode($rtn);//encodes result to json
else
echo "0 results";
It return 500, because you have error in your script. You can add below code and check where is the mistake.
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
Probably in your code is missing dot. Check this:
$str = $str . "{" .
'"ID":' . '"' . $row["ID"] . '",' .
'"Name":' . '"' . $row["Name"] . '"'
. "},";

php json not showing Wunderground station list

I'm trying to show a list of all nearby weather stations.
I have the code:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string);
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (count($station) > 1)
{
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
}
}
But currently it's not showing anything, i have searched for examples but i couldn't find any solution fot this problem.
Alternatively, you could use a simple foreach to iterate those values. Consider this example:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string, true); // <-- second parameter to TRUE to use it as an array
$desired_values = $parsed_json['location']['nearby_weather_stations']['pws']['station'];
foreach($desired_values as $key => $value) {
echo "<hr/>";
echo "City: " . $value['city'] . "<br/>";
echo "State: " . $value['state'] . "<br/>";
echo "Latitude: " . $value['lat'] . "<br/>";
echo "Longitude: " . $value['lon'] . "<br/>";
echo "<hr/>";
}
Sample Fiddle

Php Code (Get/Fetch Kongregate Games)

Can you help me there is a problem that is detect by php when execute but I think my code is okay.
The error say "Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files\wamp\www\getkongregate.php on line 46"
Website of kongregate xml: here
This is the code I have:
<?php
$xml = simplexml_load_string(file_get_contents('http://www.kongregate.com/games_for_your_site.xml'));
$json = json_encode($xml);
$games = json_decode($json);
$count = 0;
foreach ($games->game as $game) {
echo $game->id . "\n";
echo $game->title . "\n";
echo $game->thumbnail . "\n";
echo $game->launch_date . "\n";
echo $game->category . "\n";
if (array_key_exists('screenshot',$game)) {
for($i=0;$i<=(count($game->screenshot)-1);$i++) {
echo $game->screenshot[$i] . "\n";
}
}
echo $game->flash_file . "\n";
echo $game->width . "\n";
echo $game->height . "\n";
echo $game->description . "\n";
echo $game->instructions . "\n";
echo $game->gameplays . "\n";
echo $game->rating . "\n\n------\n\n";
if ($count == 100) {break;}
$count++;
}
?>
You are retreiving xml.
Why are you parsing it as json?
Use SimpleXmlElement instead and then iterate through xml elements to retreive your data.
$xml = file_get_contents($map_url);
$simpleXmlElement = simplexml_load_string($xml);
var_dump($simpleXmlElement);
Here: :)
$url = 'http://www.kongregate.com/games_for_your_site.xml';
$content = file_get_contents($url)
$xml = simplexml_load_string($content);
$count = 0;
foreach ($xml as $game) {
echo strval($game->id) . PHP_EOL;
echo strval($game->title) . PHP_EOL;
echo strval($game->thumbnail) . PHP_EOL;
echo strval($game->launch_date) . PHP_EOL;
echo strval($game->category) . PHP_EOL;
if (isset($game->screenshot) && is_array($game->screenshot)) {
foreach ($game->screenshot as $screenshot) {
echo strval($screenshot) . "\n";
}
}
echo strval($game->flash_file) . PHP_EOL;
echo strval($game->width) . PHP_EOL;
echo strval($game->height) . PHP_EOL;
echo strval($game->description) . PHP_EOL;
echo strval($game->instructions) . PHP_EOL;
echo strval($game->gameplays) . PHP_EOL;
echo strval($game->rating) . PHP_EOL;
if ($count >= 100) {
break;
}
$count++;
}

Using arrays in PHP

I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?
$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
$type[$sa[0]] = $sa[1];
}
$content = readfile("content.csv");
for($i=0; $i<sizeof($content); $i++){
if($content[$i][15] == 3){
if($content[$i][4] == 1){
$temp = $content[$i][15];
echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";
}
}
}
function readfile($filename) {
$line_of_text = array();
$file_handle = fopen($filename, "r");
while (!feof($file_handle) ) {
array_push($line_of_text, fgetcsv($file_handle, 1024));
}
fclose($file_handle);
return $line_of_text;
}
You are missing a $ sign before content:
$type[$content[$i][15]]
This accesses the value in $type with index $content[$i][15]

Categories