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

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\+/=]*");

Related

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

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");

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;

Grab 8 characters after a particular string [duplicate]

This question already has answers here:
How to extract a substring from a string in PHP until it reaches a certain character?
(5 answers)
Closed 8 years ago.
I have a long string that will have 8 characters that I need to get out of it. The 8 characters always happen after a specific number.
example:
blahblah1234WMCRCA01therestofthisisntimportant
Out of that I need to set a variable to the 8 characters after 1234 which would be WMCRCA01
This should help:
$string = "blahblah1234WMCRCA01therestofthisisntimportant";
$startingString = "1234";
$startingStringAt = strpos($string, $startingString);
if (false === $startingStringAt) {
echo "Could not find the \"$startingString\"";
} else {
$eightCharacters = substr($string, $startingStringAt + 4, 8);
echo "Eight characters found: " . $eightCharacters;
}
It is a complex solution including the case where no preceding "1234" string occurs in the base string.

Categories