Php text to byte

Php php string to bytes utf 8

Solution 2: I think you don’t need to reinvent the wheel, you could just use mb_strcut and make sure you set encoding to UTF-8 first. its return because in \xc2\x80\xc2, last one is invalid Solution 3: I coded up this simple function for this purpose, you need mb_string though. If the last byte starts with 110 (binary), drop it as well If the second-to-last byte starts with 1110 (binary), drop the last 2 bytes If the third-to-last byte starts with 11110 (binary), drop the last 3 bytes This ensures that you don’t have an incomplete character dangling at the end, which is the main thing that can go wrong when truncating UTF-8.

Читайте также:  Python pyqt5 дочерние окна

Truncate a UTF-8 string to fit a given byte count in PHP

Edit: S.Mark’s answer is actually better than mine — PHP has a (badly documented) builtin function that solves exactly this problem.

Original «back to the bits» answer follows:

  • Truncate to the desired byte count
  • If the last byte starts with 110 (binary), drop it as well
  • If the second-to-last byte starts with 1110 (binary), drop the last 2 bytes
  • If the third-to-last byte starts with 11110 (binary), drop the last 3 bytes

This ensures that you don’t have an incomplete character dangling at the end, which is the main thing that can go wrong when truncating UTF-8.

Unfortunately (as Andrew reminds me in the comments) there are also cases where two separately encoded Unicode code points form a single character (basically, diacritics such as accents can be represented as separate code point modifying the preceding letter).

Handling this kind of thing requires advanced Unicode-Fu which is not available in PHP and may not even be possible for all cases (there are somne weird scripts out there!), but fortunately it’s relatively rare, at least for Latin-based languages.

I think you don’t need to reinvent the wheel, you could just use mb_strcut and make sure you set encoding to UTF-8 first.

mb_internal_encoding('UTF-8'); echo mb_strcut("\xc2\x80\xc2\x80", 0, 3); //from index 0, cut 3 characters. 

because in \xc2\x80\xc2, last one is invalid

I coded up this simple function for this purpose, you need mb_string though.

function str_truncate($string, $bytes = null) < if (isset($bytes) === true) < // to speed things up $string = mb_substr($string, 0, $bytes, 'UTF-8'); while (strlen($string) >$bytes) < $string = mb_substr($string, 0, -1, 'UTF-8'); >> return $string; > 

While this code also works, S.Mark answer is obviously the way to go.

How to truncate an UTF8 string in PHP?, I have some strings in my PHP code that need to be truncated if they are too long. For example if a text is something like this: Hi, I would like to tell you how wonderful this is. It would re

How to convert string to UTF-8 byte array in PHP for Java BufferedInputStream.read()?

Change your inputstream. That solved my issue. Try change the rule:

in = new BufferedInputStream(socket.getInputStream()); 
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); 

On PHP side you can use utf8_encode to encode your String to UTF-8. E.g.

$message = utf8_encode($message); if ( ($fp = fsockopen($host, $port, $errno, $errstr, 3) ) === FALSE) echo "$errstr ($errno)"; else < print 'SUCCESS!
'; fwrite($fp, $message);

How to best configure PHP to handle a UTF-8 website, AddDefaultCharset utf-8 PHP (in php.ini) default_charset = «utf-8» mbstring.internal_encoding=utf-8 mbstring.http_output=UTF-8 mbstring.encoding_translation=On mbstring.func_overload=6 MySQL . CREATE your database with an utf8_* collation, let the tables inherit the database …

How to convert this string manipulation function UTF-8 Compatible in PHP?

Your problem is ucwords. A quick search on the php page made me discover this:

I tested and it works perfectly just remember this line:

Well, you’d need to swap a few functions. First, there is no str_replace alternative for UTF-8 (you may or may not need it). You should replace ucwords with mb_convert_case and strlen with mb_strlen .

But there are more efficient ways to do it than looping several times:

function my_ucwords($string) < $chrs = '"([/-'; $searchRegex = '/('.preg_quote($chrs, '/').')/u'; $replaceRegex = '/('.preg_quote($chrs, '/').')\s/u'; $tmpString = preg_replace($searchRegex, '\1 ', $string); $tmpString = mb_convert_case($tmpString, MB_CASE_TITLE); return preg_replace($replaceRegex, '\1', $tmpString); >

Utf 8 — How to convert variable (text/string) to utf8mb4 in, (PS, utf8mb4 is NOT a character encoding, utf8mb4 is just MySQL’s nickname for utf8. what MySQL calls utf8 is actually a 3-byte subset of the real utf8. and what MySQL calls utf8mb4 is the real utf8. its just some MySQL brain-damage. and unfortunately, MariaDB inherited this brain-damage from MySQL …

Источник

String to Byte Array in PHP

@Sparr is right, but I guess you expected byte array like byte[] in C#. It’s the same solution as Sparr did but instead of HEX you expected int presentation (range from 0 to 255) of each char . You can do as follows:

$byte_array = unpack('C*', 'The quick fox jumped over the lazy brown dog');
var_dump($byte_array); // $byte_array should be int[] which can be converted
// to byte[] in C# since values are range of 0 - 255

By using var_dump you can see that elements are int (not string ).

 array(44) < [1]=>int(84) [2]=> int(104) [3]=> int(101) [4]=> int(32)
[5]=> int(113) [6]=> int(117) [7]=> int(105) [8]=> int(99) [9]=> int(107)
[10]=> int(32) [11]=> int(102) [12]=> int(111) [13]=> int(120) [14]=> int(32)
[15]=> int(106) [16]=> int(117) [17]=> int(109) [18]=> int(112) [19]=> int(101)
[20]=> int(100) [21]=> int(32) [22]=> int(111) [23]=> int(118) [24]=> int(101)
[25]=> int(114) [26]=> int(32) [27]=> int(116) [28]=> int(104) [29]=> int(101)
[30]=> int(32) [31]=> int(108) [32]=> int(97) [33]=> int(122) [34]=> int(121)
[35]=> int(32) [36]=> int(98) [37]=> int(114) [38]=> int(111) [39]=> int(119)
[40]=> int(110) [41]=> int(32) [42]=> int(100) [43]=> int(111) [44]=> int(103) >

Be careful: the output array is of 1-based index (as it was pointed out in the comment)

String to byte/binary arrays in PHP

I think you are asking for the equivalent to the Perl pack/unpack functions. If that is the case, I suggest you look at the PHP pack/unpack functions:

How to convert string to ascii byte array in php

I know this is an old post now but for anyone else that comes across this in the future like me:

In PHP, strings are already byte arrays. So to put together the auth header for ProPay, see the following example:

$certStr = 'MyCertStr';
$termId = 'MyTermId';
$base64 = base64_encode($certStr.':'.$termId);
$authorization = 'Basic '.$base64;
//Output: "Basic TXlDZXJ0U3RyOk15VGVybUlk"

PHP — Create a valid byte string

$codes = ["02", "66", "6c", "6a", "3a", "03"];
$byteString1 = hex2bin(implode('', $codes));

How do I get the byte values of a string in PHP?

$var = "nÖ§9q1Fª£ˆæÓ§Œ_»—Ló]j";

for($i = 0; $i < strlen($var); $i++)
echo ord($var[$i])."
";
>
?>

php string as byte array, hashed then base64 encoded

Yes, strings in PHP are already byte arrays. And unless your $string contains any non-ASCII characters, it’s also already valid UTF-8 (UTF-8 is a superset of ASCII); so you can skip the «encode as UTF-8» step.

Likely the algorithm is expecting the output of the hash to be binary, which you’re then supposed to convert to base 64. By default hash returns hex values, not binary. For that you need to set its 3rd parameter. In summary:

$unique = uniqid();
$string = $unique . ':' . $this->apiKey;
$hash = hash('sha256', $string, true);
$base64 = base64_encode($hash);

Of course, what you’re supposed to do with $unique I don’t know. Likely you’re supposed to send that value together with the request as well, otherwise there’s no way the server can validate the hash.

Base64 of byte array in Java and it’s equivalent in PHP

PHP will already treat $string as a byte string, so you don’t need to unpack/implode it.

$string = 'ec65450a-5:5217e';
$bytes = unpack('C*', $string);
echo implode('', $bytes);
1019954535253489745535853504955101

Which is a mushed together list of integer base 10 ASCII values of each character, and is almost certainly not what you want. Just encode the string directly:

Also, you’ll want to change your password now that you’ve posted it here. 🙂

How can I convert array of bytes to a string in PHP?

If by array of bytes you mean:

$bytes = array(255, 0, 55, 42, 17, );

array_map()

$string = implode(array_map("chr", $bytes));

foreach()

Which is the compact version of:

$string = "";
foreach ($bytes as $chr) $string .= chr($chr);
>
// Might be a bit speedier due to not constructing a temporary array.

pack()

But the most advisable alternative could be to use pack(«C*», [$array. ]) , even though it requires a funky array workaround in PHP to pass the integer list:

$str = call_user_func_array("pack", array_merge(array("C*"), $bytes)));

That construct is also more useful if you might need to switch from bytes C* (for ASCII strings) to words S* (for UCS2) or even have a list of 32bit integers L* (e.g. a UCS4 Unicode string).

Источник

How to Get the Byte Values of a String in PHP

How can I get the single bytes from a multibyte PHP string variable in a binary-safe way?

you can get a bytearray by unpacking the utf8_encoded string $a:

$a = utf8_encode('Fön');
$b = unpack('C*', $a);
var_dump($b);

used format C* for «unsigned char»

  • String to byte array in php
  • http://www.php.net/manual/en/function.unpack.php
  • http://www.php.net/manual/en/function.pack.php

String to byte array in php

@Sparr is right, but I guess you expected byte array like byte[] in C#. It’s the same solution as Sparr did but instead of HEX you expected int presentation (range from 0 to 255) of each char . You can do as follows:

$byte_array = unpack('C*', 'The quick fox jumped over the lazy brown dog');
var_dump($byte_array); // $byte_array should be int[] which can be converted
// to byte[] in C# since values are range of 0 - 255

By using var_dump you can see that elements are int (not string ).

 array(44) < [1]=>int(84) [2]=> int(104) [3]=> int(101) [4]=> int(32)
[5]=> int(113) [6]=> int(117) [7]=> int(105) [8]=> int(99) [9]=> int(107)
[10]=> int(32) [11]=> int(102) [12]=> int(111) [13]=> int(120) [14]=> int(32)
[15]=> int(106) [16]=> int(117) [17]=> int(109) [18]=> int(112) [19]=> int(101)
[20]=> int(100) [21]=> int(32) [22]=> int(111) [23]=> int(118) [24]=> int(101)
[25]=> int(114) [26]=> int(32) [27]=> int(116) [28]=> int(104) [29]=> int(101)
[30]=> int(32) [31]=> int(108) [32]=> int(97) [33]=> int(122) [34]=> int(121)
[35]=> int(32) [36]=> int(98) [37]=> int(114) [38]=> int(111) [39]=> int(119)
[40]=> int(110) [41]=> int(32) [42]=> int(100) [43]=> int(111) [44]=> int(103) >

Be careful: the output array is of 1-based index (as it was pointed out in the comment)

PHP Read Byte Range from String or File

You should be able to do this with fseek and fread .

$byteOffset = 1024;
$readLength = 256;
$fileHandle = fopen('myfile', 'r');
fseek($fileHandle, $byteOffset);
$bytes = fread($fileHandle, $readLength);

String to byte/binary arrays in PHP

I think you are asking for the equivalent to the Perl pack/unpack functions. If that is the case, I suggest you look at the PHP pack/unpack functions:

How can I convert array of bytes to a string in PHP?

If by array of bytes you mean:

$bytes = array(255, 0, 55, 42, 17, );

array_map()

$string = implode(array_map("chr", $bytes));

foreach()

Which is the compact version of:

$string = "";
foreach ($bytes as $chr) $string .= chr($chr);
>
// Might be a bit speedier due to not constructing a temporary array.

pack()

But the most advisable alternative could be to use pack(«C*», [$array. ]) , even though it requires a funky array workaround in PHP to pass the integer list:

$str = call_user_func_array("pack", array_merge(array("C*"), $bytes)));

That construct is also more useful if you might need to switch from bytes C* (for ASCII strings) to words S* (for UCS2) or even have a list of 32bit integers L* (e.g. a UCS4 Unicode string).

Источник

Оцените статью