I'd like to ask dumb question.
How could I convert #48#49#50 based on # char ?
In PHP, understand that chr() function is used to convert a ASCII value to a character.
48 is 0.
49 is 1.
And 50 is 2.
May I know how to convert #48#49#50 as 012 and how to store 012 in one variable ?
Eg- $num = 012
We can try using preg_replace_callback() here with the regex pattern #\d+. As we capture each #\d+ match, use chr() on that match to generate the ASCII character replacement.
$input = "#48#49#50";
$out = preg_replace_callback(
"/#(\d+)/",
function($m) { return chr($m[1]); },
$input
);
echo $out; // 012
Related
I want to remove all characters with ascii codes 32 - 47 and some more from string. Exactly !"#$%&'()*+,-./\~.
I tried:
$string = preg_replace('/\s\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\\~/', '', $string);
But it returned false. What am I doing wrong? Thanks.
To use the characters just include them in a character class:
$string = preg_replace(':[\s!"#$%&\'()*+,-./\\\~]:', '', $string);
Or use ASCII hexadecimal for the range and characters:
[\x20-\x2f\x5c\x7e]
Or use the actual characters in a range as long as you start with the first (space) and end with the last / in the range and then add the rest:
[ -/\\\~]
I need do a regex match for ASCII characters 32 - 90 inclusive.
I've attempted the following, however it doesn't seem to return anything.
preg_match("/^[\x20-\x5A]+$/u", $input)
The idea is that it is from hex 20 to hex 5a. I pulled these from http://www.asciitable.com/
I've got a spot for testing this on http://www.phpliveregex.com/p/2Dh
Your current range only supports upper case letters, so you need the /i modifier:
$input = 'adddd ### AAAA????';
preg_match('/^[\x20-\x5A]+$/i', $input); // int(1)
Alternatively, add the extra letters in the range:
preg_match('/^[\x20-\x5A\x61-\x7A]+$/', $input))
You need to use preg_match's third parameter to assign it to a variable
preg_match("/^[\x20-\x5A]+$/u", $input, $matches)
The standard return of this function is a 1 or 0/FALSE
eg...
if(preg_match("/^[\x20-\x5A]+$/u", $input, $matches))
{
var_dump($matches);
}
I have an japanese english character.
This character is not normal english string.
Characters: Game
How to transform this character to normal english character in php?
Subtract 65248 from the ordinal value of each character. In other words:
$str = "Game some other text by ヴィックサ";
$str = preg_replace_callback(
"/[\x{ff01}-\x{ff5e}]/u",
function($c) {
// convert UTF-8 sequence to ordinal value
$code = ((ord($c[0][0])&0xf)<<12)|((ord($c[0][1])&0x3f)<<6)|(ord($c[0][2])&0x3f);
return chr($code-0xffe0);
},
$str);
This will replace all of the "Fullwidth" characters with their normal width equivalents.
It would be easier to use mb_convert_kana:
$string = 'Characters: Game';
$newString = mb_convert_kana($string,'a');
I'm sure there is a much easier answer but couldnt you make a dictonary object with the special charter as the key and the char you want as the value
then just do a simple find and replace?
If I have this PHP string:
$string = '\\x27\\x22';
How would I decode it to '"?
A regex could help you here:
$out = preg_replace_callback(
"(\\\\x([0-9a-f]{2}))i",
function($a) {return chr(hexdec($a[1]));},
$string
);
You do not need to decode it. Just do str_replace('\\x27', "'", $str);. In case your '" was just and example, please note you got repeatable pattern \\xAA, where x indicates hexadecimal notation and AA is hex value itself, so each \\xAA represents single byte and AA is from 0 to 0xFF. So you can use regexp or just walk any other way over your string, extract these AA values and convert it with chr(hexdec($AA)) to coresponding characted and glue with result string.
$out = preg_replace_callback(
"(\\\\x([0-9a-f]{2}))i",
function($a) {return '\u00'.bin2hex(hex2bin($a[1]));},
$string
);
That's ok after I converted the value from ascii to unicode.
Basically I have this string for example $str = 'По, своей 12' I need something that returns the numbers of chars and numbers (leaving off spaces and other punctuation)
How can I achive this? Maybe doing a preg_replace with \p{L} and [0-9] ?
countChars('По, своей 12'); //> Should return: 9
Note: mb_strlen() counts spaces and punctuation too and i don't want this
I did it:
function countChars($haystack) {
$count = preg_replace("/[^\p{L}0-9]/uim",'',$haystack);
return mb_strlen($count);
}