Compare 2 strings with different encoding - php

How to make the following strings equal:
$str1 = "Première équation";
$str2 = "Première équation";
I tried html_entity_decode($str1) but it doesn't work

I'd use a combination of strcmp and html_entity_decode for binary-safe comparison.
<?php
$str1 = "Première équation";
$str2 = "Première équation";
var_dump( strcmp(html_entity_decode($str1), $str2) );
https://eval.in/551298
For example;
// Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
if( strcmp(html_entity_decode($str1), $str2) === 0 ) {
echo "They match";
}

Related

PHP strcmp result int meaning

In php, there is a built-in function strcmp to compare if two strings are same or not.
Returning value is integer number that if the first parameter is greater than the second I get > 0, if not < 0 and if the same 0.
So the part I don't get is comparing string as number.
Does PHP convert string to number and if so how's PHP converting?
$a = 'acorn';
$b = 'zebra';
var_dump( strcmp($a, $b) ); // -25 <- what's this number? seems like alphabetical position...nnn
Doesn't it really matter what number I get, shall I just take what it is?
Looking at the PHP: strcmp doc :
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than
str2, and 0 if they are equal.
So yes, you can use it as it is to compare your string.
But if you want to understand the number returned by the function, it depend on the characters that makes the strings.
In ASCII :
A=65 < B=66 < C=67 ....
So if the string are different, one is gonna be greater than the other.
So you can also test it easily with a short script :
<?php
$a='A';
$b='B';
$c='C';
//Return -1 because $a is smaller than $b by one (65 < 66 )
echo strcmp($a,$b);
//Return -2 because $a is smaller than $c by two (65 < 67 )
echo strcmp($a,$c);
//Return -1 because $b is smaller than $c by one (66 < 67 )
echo strcmp($b,$c);
//Return 1 because $c is greater than $b by one (67 > 66 )
echo strcmp($c,$b);
//Return 2 because $c is greater than $a by two (67 > 65 )
echo strcmp($c,$a);
strcmp is using for comparing two strings in PHP.
If your result is greater the 0 its mean variable one ($a) is greater then Var2($b)
if return result is less then 0 its mean $b is greater and If result is equal to 0 its mean both strings are equal to each other.
$a="Hello world!";
$b= "Hello world!";
var_dump(strcmp($a, $b)); // result is 0
$a = "Hello";
$b = "Hello World";
var_dump(strcmp($a, $b)); // Outputs: -6
and If you want to see difference then you can use this
$a = 'some-content-here-example';
$b = 'some-content-example';
$asp = preg_split('//', $a, -1);
$bsp = preg_split('//', $b, -1);
$l1 = count($asp);
$l2 = count($bsp);
$length = $l1;
if ($l2 > $l1) {
$length = $l2;
}
$record = null;
$x = null;
for ($x = 0; $x < $length; $x++) {
if ($a[$x] != $b[$x]) {
if (!isset($a[$x])) {
$record.= $b[$x];
} else {
$record.= $a[$x];
}
}
}
echo $record;

In PHP, what does (int) really do?

I understand that casting with (int) rounds a number towards zero, but then I started poking around with bases other than 10 and now I'm not exactly sure what else (int) does:
$a = 050; // an octal number
echo $a; // output is ----> 40
however
$a = '050';
echo (int)$a; // output is ----> 50
So I thought (int) besides rounding towards zero also clips leading zeroes. But then if I do the same exercise as in the second code block (above) with binary I don't get what I expect:
$a = '0b010';
echo (int)$a; // output is ----> 0
and in hexadecimal:
$a = '0x050';
echo (int)$a; // output is ----> 0
Even using decimal scientific notation gives a quizzical result:
$a = 5e6;
$b = '5e6';
echo $a; // output is ----> 5000000
echo (int)$b; // output is ----> 5
What does casting to an integer using (int) really mean? Besides stripping the decimal point and everything after in a float, does (int) parse a cast string character-by-character, and return the concatenation of all leading, numeric, non-zero characters? Like this:
function intCast($str) {
$length = strlen($str);
$returnVal = '';
for($i = 0; $i < $length; $i++) {
// Note: ord() values 48 to 57 represent 0 through 9
if ($returnVal === '' && ord($str[$i]) == 48) {
continue; // leading zeroes are OK but don't concatenate
} else if (ord($str[$i]) > 48 && ord($str[$i]) < 58) {
$returnVal .= $str[$i];
} else {
break;
}
}
return (strlen($returnVal) > 0) ? $returnVal : 0;
}
***Note that this link does not seem to provide a sufficient answer.
'050' is a string, and PHP's first job in doing a typecast is to normalize that string:
echo '050' + 1 -> echo '50' + 1 -> echo 50 + 1 -> echo 51 -> 51
050 by itself is an octal number, it's already an integer, so the only thing PHP has to do is convert it to a base10 number for output:
echo 050 + 1 -> echo 40 + 1 -> echo 41 -> 41

How to check if a number (float or integer) is within a range (0 - 100)

I'm looking for the fastest way to get this test.
So functions, operands and everything else is allowed.
I tried with the following regex (I'm not an expert):
0\.[0-9]+|100\.0+|100|[1-9]\d{0,1}\.{0,1}[0-9]+
It works except that it erroneously accept 0.0 or 0.000000 and so on.
Also it's not the most appropriated and fastest way.
(if anybody wants to fix the regex to don't allow those 0.00 values it would be appreciated)`
No need for regex:
if (is_numeric($val) && $val > 0 && $val <= 100)
{
echo '$val is number (int or float) between 0 and 100';
}
Demo
Update
It turns out you're getting the numeric values from a string. In that case, it would be best to extract all of them using a regex, something like:
if (preg_match_all('/\d+\.?\d*/', $string, $allNumbers))
{
$valid = [];
foreach ($allNumbers[0] as $num)
{
if ($num > 0 && $num <= 100)
$valid[] = $num;
}
}
You can leave out the is_numeric check, because the matched strings are guaranteed to be numeric anyway...
Use bccomp
This is a perfect use case for BCMath functions.
function compare_numberic_strings($number) {
if (
is_numeric($number) &&
bccomp($number, '0') === 1 &&
bccomp($number, '100') === -1
) {
return true;
}
return false;
}
echo compare_numberic_strings('0.00001');
//returns true
echo compare_numberic_strings('50');
//returns true
echo compare_numeric_strings('100.1');
//returns false
echo compare_numeric_strings('-0.1');
//returns false
From the manual:
Returns 0 if the two operands are equal, 1 if the left_operand is
larger than the right_operand, -1 otherwise.
I think your regex pattern should look like this:
^\d{1,2}$|(100)
Demo

comparing two strings and determining if they are equal

I have two strings that am trying to compare but it is displaying that they are not equal. What could be the problem? Here is the code that am running.
<?php
$x = "Come and enjoy the show.";
$y = "Come and enjoy the show.";
if (strcmp($x, $y)) {
echo "They are the same.";
} else {
echo "They are not the same.";
}
?>
strcmp - It returns zero on exact match & hence else condition will get executed in your case.
Defination: Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
Change your condition with,
if (strcmp($x, $y) === 0) {
echo "They are the same.";
} else {
echo "They are not the same.";
}
DEMO.
strcmp can returns multiple values.
From the doc :
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
So, instead try this code :
if (strcmp($x, $y) === 0) {
echo "They are the same.";
} else {
echo "They are not the same.";
}
?>
try this...
strcmp function
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
if (strcmp($x, $y) === 0) {
echo "They are the same.";
} else {
echo "They are not the same.";
}

String comparison is not working in php

I'm very new to php. I have a json named json. When I try to do this:
echo $json->status;
I get :
CREATED
I try to compare this result with normal string CREATED like this:
if(strcasecmp("CREATED",$json->status))
{
print_r("Order created successfuly");
}
but for some reason the if condition is not evaluting to true. Even though I compare CREATED with CREATED!
Not sure where the error is.
Thanks in advance.
This function return zero if strings are equal
if (strcasecmp("CREATED",$json->status) == 0)
Look to the manual:
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
so strcasecmp('a','a') is 0, therefore you have to change your code into
if(strcasecmp("CREATED",$json->status) == 0)
{
print_r("Order created successfuly");
}
http://php.net/manual/en/function.strcasecmp.php
Quote from the page :
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
So strcasecmp('CREATED', 'CREATED') returns 0. And 0 is not equals to true.
You must do that :
if (strcasecmp("CREATED",$json->status) === 0) {
print_r("Order created successfuly");
}
if (strcasecmp( $json->status, "CREATED") == 0)
{
...
...
}
Why cant you just use a simpler if statement?
if( $json->status == "CREATED" ) {
print_r("Order created successfuly");
}
And check for whitespaces at the end or start of the status.
To compare strings, try to do the following :
if($json->status == "CREATED")
{
echo "Order created successfuly";
}

Categories