Convert a hex string to ascii string in php [duplicate] - php

This question already has answers here:
Convert hex to ascii characters
(5 answers)
Closed 3 years ago.
I want to convert a "hex" String: $hexstring = "8F21FF434927";
into its ascii code: !ÿCI'
I found simple hex2str Methodes but they do an output like: 0x8F 0x21...

function hexToStr($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
echo hexToStr("8F21FF434927");

Related

PHP convert string to "\xnn" hex [duplicate]

This question already has answers here:
PHP convert string to hex and hex to string
(8 answers)
Encoding/decoding string in hexadecimal and back
(2 answers)
Closed 4 years ago.
How can i convert string 'Hello world' to '\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21' using php?
Thanks!
Should work
function strhex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
Update
Code below what you need
function strtohex($string)
{
$string = str_split($string);
foreach($string as &$char)
$char = "\x".dechex(ord($char));
return implode('',$string);
}
print strtohex("[0-9A-Za-z\+/=]*");

Remove everything right of a match in a variable in PHP [duplicate]

This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC

Removing all characters before certain string [duplicate]

This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC

How can I find a special character in my string and store all value after this character in a variable [duplicate]

This question already has answers here:
How to get everything after a certain character?
(9 answers)
Closed 8 years ago.
I am trying to find special character $# from my string and trying to store all value after these special in a variable but I could not do this task. How do I find this $# special character in my whole string and store all value after this special charecter in a variable. My string is:
92~SAPL/1200/2012~2~LAXMAN SINGH AND OTHERS~SHANKER SINGH AND OTHERS~D~
2014-04-10~09:31:13
07/04/2014|93~SAPL/275/1998~1~RAM SWAROOP AND OTHERS~BABU RAM AND OTHERS~D~
2014-04-10~09:31:13 07/04/2014|94~FAFOD/52/2013~3~RAM KUMAR PANDEY~RAJENDRA PANDEY AND
ANOTHER~D~2014-04-10~09:31:13 07/04/2014$#2014-04-10~2014-04-09
try this:
$string = "Your Entire String";
$explodedString = explode('$#', $string);
if(count($explodedString) > 1) {
unset($explodedString[0]);
$returnString = count($explodedString) > 1 ? $explodedString : $explodedString[0];
} else {
$returnString = null;
}
if(is_array($returnString)) {
//if you want to divide the string only to the first occurrence of $# leave the below line as is else comment it out
$returnString = implode('', $returnString);
}
echo $returnString;

How to transfer `\u0627` into utf-8(php) [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to decode Unicode escape sequences like “\u00ed” to proper UTF-8 encoded characters?
$str = '\u0627\u0644\u0631\u0626\u064a\u0633';
How to transfer it into utf-8? thanks.
$str = '\u0627\u0644\u0631\u0626\u064a\u0633';
$converted = preg_replace('/\\\u([0-9a-z]{4})/i', '&#x$1;', $str);
echo $converted; // displays الرئيس
$str = '"\u0627\u0644\u0631\u0626\u064a\u0633"';
echo json_decode($str);

Categories