Currency symbol is not displaying properly in PHP - php

Currency symbol is not displaying properly.
Below is my code:
$total = "₮50.40";
echo $total."<br>"; //output: ₮50.40
$str3 = substr($total, 0, 1);
echo $str3; //output: �
The variable $total is displaying correctly. But I extracted the symbol from $total and display it, unfortunately it shows � .
I want to display ₮ from variable $total.
I tried utf8 encoding, but no luck.

The ₮ character is a multi-byte character, so you need to use mb_substr, not substr:
$total = "₮50.40";
$str3 = mb_substr($total, 0, 1);
echo $str3;
Output:
₮
Demo on 3v4l.org

Related

Create UTF-8 code from dynamic Unicode in PHP

I am making a dynamic Unicode icon in PHP. I want the UTF-8 code of the Unicode icon.
So far I have done:
$value = "1F600";
$emoIcon = "\u{$value}";
$emoIcon = preg_replace("/\\\\u([0-9A-F]{2,5})/i", "&#x$1;", $emoIcon);
echo $emoIcon; //output 😀
$hex=bin2hex($emoIcon);
echo $hex; // output 26237831463630303b
$hexVal=chunk_split($hex,2,"\\x");
var_dump($hexVal); // output 26\x23\x78\x31\x46\x36\x30\x30\x3b\x
$result= "\\x" . substr($hexVal,0,-2);
var_dump($result); // output \x26\x23\x78\x31\x46\x36\x30\x30\x3b
But when I put the value directly, it prints the correct data:
$emoIcon = "\u{1F600}";
$emoIcon = preg_replace("/\\\\u([0-9A-F]{2,5})/i", "&#x$1;", $emoIcon);
echo $emoIcon; //output 😀
$hex=bin2hex($emoIcon);
echo $hex; // output f09f9880
$hexVal=chunk_split($hex,2,"\\x");
var_dump($hexVal); // output f0\x9f\x98\x80\x
$result= "\\x" . substr($hexVal,0,-2);
var_dump($result); // output \xf0\x9f\x98\x80
\u{1F600} is a Unicode escape sequence used in double-quoted strings, it must have a literal value - trying to use "\u{$value}", as you've seen, doesn't work (for a couple reasons, but that doesn't matter so much.)
If you want to start with "1F600" and end up with 😀 use hexdec to turn it into an integer and feed that to IntlChar::chr to encode that code point as UTF-8. E.g.:
$value = "1F600";
echo IntlChar::chr(hexdec($value));
Outputs:
😀

how to convert from base64 to hexadecimal in php?

I was thinking to use this:
<?php
$string1 = 'V2h5IEkgY2FuJ3QgZG8gdGhpcyEhISEh';
echo base64_decode($string1);
?>
The output for this example should always be 18 characters! But sometimes this output is less than 18.
24 (base64 characters) multiplied by 6 (bits per base64 character) equals to 144 (bits) divided by 8 (bits per ASCII character) equals to 18 ASCII characters.
The problem is that the output is displayed in plain text; and some characters don't even have a "text representation" and that data will be lost. The next test will show that there are 41 different ASCII characters with no visible output.
<?php
for ($i = 0; $i <= 255; $i++) {
$string2 = chr($i);
echo $i . " = " . $string2 . "<br>";
}
?>
My plan was to decode the base64 string and from the output in ASCII reconvert it to hexadecimal. Now that is not possible because of those 41 characters.
I also tried base_convert but there is no base64 support for it.
You can do this with bin2hex():
Returns an ASCII string containing the hexadecimal representation of str. The conversion is done byte-wise with the high-nibble first.
php > $string1 = 'V2h5IEkgY2FuJ3QgZG8gdGhpcyEhISEh';
php > echo base64_decode($string1);
Why I can't do this!!!!!
php > echo bin2hex(base64_decode($string1));
57687920492063616e277420646f20746869732121212121
php >
<?php
$string1 = 'V2h5IEkgY2FuJ3QgZG8gdGhpcyEhISEh';
$binary = base64_decode($string1);
$hex = bin2hex($binary);
echo $hex;
?>
A simple one line solution is:
<?=bin2hex(base64_decode($string1));?>

String cannot start with Å, Ä, Ö

Why is it, when i shorten a string. Letter "å, ä, ö" becomes "?"?
If i use the Name "Örjan" it becomes "Orjan".
But when i use "Björn", it works all fine?
PHP
//Create initials
$usr_fname_f_letter = $_POST['usr_fname'];
$usr_fname_f_letter = $usr_fname_f_letter[0];
$usr_lname_f_letter = $_POST['usr_lname'];
$usr_lname_f_letter = $usr_lname_f_letter[0];
$usr_inits = $usr_fname_f_letter .= $usr_lname_f_letter;
echo $_POST['usr_fname'];
echo '<br>';
echo $_POST['usr_lname'];
echo '<br>';
echo $usr_fname_f_letter;
echo '<br>';
echo $usr_lname_f_letter;
echo '<br>';
echo $usr_inits;
echo '<br>';
RESULT
Örjan
Björnsson
�B
B
�B
$usr_fname_f_letter = $usr_fname_f_letter[0];
simply takes the first (zero offset) byte from $usr_fname_f_letter; but you're using a multibyte character set and that's like chopping part of a character in half.
Use
mb_substr($usr_fname_f_letter, 0, 1, 'UTF-8')
because the mb_* functions are multi-byte character set aware; and work in characters, not in bytes
I assume your encoding is utf-8 and you are probably printing only part of a multibyte character.
Try to use multibyte safe function, like mb_substr:
mb_substr($str, 0, 1, "UTF-8");

Price formatting for Portuguese

On my website, echo $symbols['currency']; echo $fields['price']; holds a value in US currency format and it outputs R$19800
and echo $symbols['currency']; echo number_format($fields['price']); outputs R$19,800
How do I format it to output R$19.800,00 which is Portuguese price formatting?
I tried
echo $symbols['currency'];
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $fields['price']);
and it outputs R$EUR 19.800,00 which is corrent, but Im finding it hard to remove EUR from printing. Thanks a lot.
Simply use the PHP function str_replace:
$money = money_format('%.2n', $fields['price']);
echo str_replace("EUR", "", $money);
You can do this without locale and str_replace() (which I consider just fixing/hiding something done not correctly) by just using number_format():
echo $symbols['currency'];
echo number_format((float) $fields['price'], 2, ',', '.');

How to convert Arabic text to Hex using PHP

Kindly I need to convert the Arabic text to and from Hexadecimal like the following example Using PHP
مرحبا
06450631062D06280627
Regards,
Eco
If you just need to have the Arabic text written in the HTML document begin generated, I think the simplest way is to convert the sequence to character references, turning e.g. 0645 to م. This could be done as follows:
<?php
$str = '06450631062D06280627';
for($i = 0; $i < strlen($str)/4; $i++) {
echo "&#x", substr($str, 4*$i, 4), ";";
}
?>
I get a unicode string with following code.
$str = "Some Hexa String";
$replacedString = preg_replace("/\\\\u([0-9abcdef]{4})/", "&#x$1;", $str);
$unicodeString = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');
bin2hex($str); // Bin to Hex
pack("H*", $hexStr); // Hex to Bin

Categories