php string comparison not working on == - php

i have question on string comparison i have
if($decryptedname == $plain)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
in a for loop to go through a text file, decryptedname contain a string "Lee" and plain contain the data in my text files and printing line by line, since both of it contain a field with the value "Lee" i assume it should match and print success as well as the $decryptedname but it's not, below is the copy and pasted result, where lee is $decryptedname and those others are echo by $plain thru a for loop
Lee
Decrypted name is Lee
Decrypted email is mjlee181#hotmail.com
Decrypted message is testing

<?php
if (strcasecmp($decryptedname , $plain) == 0) {
echo "success <br/>";
echo $_SESSION['decryptedname'];
?>
you can try this .May it help you

There are chances for whitespace getting added when you load contents from a text file , so in that case always do a trim() and then do the comparison.
Like this..
if(trim($decryptedname) == trim($plain))
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
or if you are trying to check if the string contains in another string... you should be doing stripos() instead.
if(stripos($decryptedname,$plain)!==false)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}

The above mentioned problem may be caused due to the '\n' present at the end of the string.
If the string "Lee" is present at the end of the line in the text file, it may automatically be appended with '\n' in the end. So if you remove '\n' from the end it might work fine. you can use the following function to do that:
$plain=str_replace("\r\n","",$plain);

Related

PHP string comparison to a txt

The $search is a string of variables made from text inputs in a form. I am looking to see if that string is found in a txt file. I think something is wrong with my regex but I am not sure.
An existing entry to the text file would look like this Title%Author%ISBN%Publisher%Year.
My issue is that when I submit the form it goes to a blank page.
elseif ($inquiry=='search') {
$file= fopen("database.txt", "r") or die("File was not found on server");
$search = "/^[$Title."%".$Author."%".$ISBN."%".$Publisher."%".$Year]/i";
//search function
// What to look for
// open and Read from file
$lines = file('database.txt');//array
foreach($lines as $line) {
// Check if the line contains the string we're looking for, and print if it does
if(preg_match($search, $line)) {
echo $line;
} else {
echo "Search not found";
}
}
}
fclose($file);
}
You have to be aware of two things first.
The special $ char in PHP is used to denominate a variable.
When you inject a word with a preceding $ in a double quoted string, this is treated as a variable name and the variable tries to expand itself.
I'm mentioning this because of this line:
$search = "/^[$Title."%".$Author."%".$ISBN."%".$Publisher."%".$Year]/i";
So, my best guess there is that you are trying to use and expand the variable names. So, if that's the case you are ok on your intention.
But be aware you have a missmatched and no closed " after the Title.
Also remember the $ has a special meaning in regular expressions, and they are usually used to try to match the "end of the line".
Note: Your script is probably dying due to a missing " after the Title.

PHP - If statement comparing string with contents of txt file

I did some research and couldn't find a clear answer to my problem. This is what I have:
<?php
session_start();
$gen_num = file_get_contents($_SESSION['filename']);
$inp_num = $_POST['form-input'];
if($gen_num === $inp_num){
echo "Yes! The numbers match!";
} else {
echo "No, the numbers do not match…";
}
?>
Where the 'filename' has a string of numbers and 'form-input' is carried from a previously submitted HTML form.
Why does the IF test fail when the strings are identical?
EDIT
The simple fix was trimming the $gen_num. Also, I wasn't specific enough in saying that the 'filename' was a .txt file that included a string of numbers along with some unseen special characters.
Thank you for the help!
If $_SESSION['filename'] is as you say a string of numbers then there is no file name ending.
You should make that line:
$gen_num = file_get_contents($_SESSION['filename'] . ".txt");
Or file_get_contents won't find a file with only numbers as the name and return false.
That is why the numbers don't match.
If the two values are integers I'd floor() them and use ==
And catch for 0 exception

newline in json_encode() output

I am building an output array like so
if (count($errors)) {
$success = 'false';
$output['json_msg'] = "Please try your submission again.";
$output['errors'] = $errors;
} else {
$success = 'true';
$output['json_msg'] = "Thanks for Becoming a NOLA Insider!";
}
$output['success'] = $success;
header('Content-type:application/json;charset=utf-8');
if (count($errors)) { http_response_code(500); }
echo json_encode($output);
exit;
But when I look at the response in Chrome's Network pane of the developer tools I see what appears to be a newline in response:
I tried wrapping json_encode() in trim() but this gave garbled output.
How do I eliminate the carriage return?
You can try to remove new line using str_replace
$output = str_replace(array("\r\n", "\n", "\r"),'',$output);
echo json_encode($output);
Do you have a ?> at the end of your PHP file and what's happening when you remove it ?
Because you may have a carriage return at the end of the script which may be sent before your response :
?>\n
// END OF FILE
This is explained by the fact that PHP is actually a templating language :
Here is a file which defines a function and which displays a text :
<?php
/**
* #File lib.php
*/
function sayHello()
{
echo "hello";
}
?>
forgotten text
And here is a file that includes this file.
<?php
/**
* #file index.php
*/
include_once('lib.php');
sayHello();
This will output :
forgotten text
hello
The "forgotten text" is output when the lib.php file is included whereas the "hello" is output after.
(But it may be even simpler and just the point that #nanocv suggested)
If you are getting the new line characters like \r\n to your json code after json_encode() you can follow up the method with the final json_value that you get. This will remove up all the new lines that has been output-ed from the code that you obtain after you perform the json_encode().
Hence you need to preg_replace() the json outputed value as follows which will remove uo the new lines from the json_code.
This will replace the new lines with no value over to the second parameter in preg_replace().
Try not to provide any white spaced between the php codes (i.e) Opening and Closing Codes that you process either at the beginning or at the end of the document. This may cause the issue sometimes.
Code:
$output_json = preg_replace("!\r?\n!","", $output_json);
I bet your php code starts this way:
1. <--- Note the blank line here
2. <?php
That's a new line character that will became part of the result.
(This way I could recreate the same behavior)
I solved by removing spaces from the php file indicated in the include .

String Comparison Issue with xml and php

I have a problem with xml in php... when I say echo $meta2->nodeValue; It shows Address but when I say if ($meta2->nodeValue=="Address"){echo $meta2->nodeValue;} it does not show anything... Any ideas? nodeType=1
You should trim the value to be sure to avoid all non-visible character:
if (rtrim($meta2->nodeValue) == "Address")
{
echo $meta2->nodeValue;
}

if statement not reading variables

I wrote a program that reads data from a csv (saved as Unicode format in SQL Studio) and then does a few comparisons and prints out the results.
For some reason this simple if statement returns false eventhough when the data is printed it shows the correct results:
foreach ($complete as $key => $val){
$INVOICE = 'INVOICE';
$InvoiceType = $complete[$key][9];
echo 'comparing' . $InvoiceType.'to'. $INVOICE;
if ($InvoiceType == $INVOICE){
echo 'I am inside a invoice';
}
else if ($InvoiceType == 'CREDIT'){
$PayeeDocumentType = 'CM';echo 'I am inside a credit';
}
}
--------- Results ---------
comparingINVOICEtoINVOICEcomparingINVOICEtoINVOICE
My only guess is that $complete[$key][9] is an object and not a string.
What happens if you add print_r($complete[$key][9]) somewhere or modify the 2nd line of code to:
$InvoiceType = (string) $complete[$key][9];
First guess try
$InvoiceType = trim($complete[$key][9]);
Also note the output of the var_dump in "view source" of the page. Basically see the raw output bring sent to the browser, maybe there are some invisible characters there.

Categories