To Convert String to Blob in PHP we have to use PHP built-in functions, here is the list of PHP functions which we used,
- ord() function
- decbin() function
- bindec() function
- chr() function

ord() function is a PHP inbuilt function that used to convert the first character of a string to its ASCII value.
decbin() function is also a inbuilt PHP function and used to convert decimal to binary number.
Main Concept of Convert String to Blob With Example
First, convert string character to ASCII value using
ord()
function then convert ASCII value or decimal number to a binary value usingdecbin()
function.
Source Code:
1 2 3 4 5 6 7 8 |
<?php function string_to_blob($str){ $bin = ""; for($i = 0, $j = strlen($str); $i < $j; $i++) $bin .= decbin(ord($str[$i])) . " "; echo $bin; } string_to_blob("PHPCODER"); |
Output:
1010000 1001000 1010000 1000011 1001111 1000100 1000101 1010010
You can compile the complete code here: https://phpcoder.tech/php-online-editor/
Convert Blob to String With Example
Here are 2 main function which we used to convert.
bindec() function is used to convert a binary number to an integer number and this is also a PHP inbuilt function.
chr() function is used for getting the character from specified ASCII value.
Source Code:
1 2 3 4 5 6 7 8 9 |
<?php function blob_to_string($bin){ $char = explode(' ', $bin); $userStr = ''; foreach($char as $ch) $userStr .= chr(bindec($ch)); return $userStr; } echo blob_to_string("1010000 1001000 1010000 1000011 1001111 1000100 1000101 1010010"); |
Output:
PHPCODER
Main Points of Code
- To convert both we use a PHP loop for taking one by one character convert it.
- And we use the above main PHP inbuilt functions used to complete conversion.
I hope you will understand all the things correctly.
More about blob with MySQL: https://www.php.net/manual/en/function.fbsql-create-blob.php
Also Check:
- How to Convert JSON to Array PHP
- Check if JavaScript variable is NULL or Undefined
- Get List of Holidays Using Google Calendar API
- Integrate Google reCaptcha in Codeigniter
Happy Coding..!