I need to invent my own conversion function because of some encoding issue.
I decided that for now I will create a conversion table for my characters.
I want to know how I can possibly do an operation like the following C code to print out characters 'a' to 'z':
char a='a';
for(i=0;i<26;i++){
printf("%c",a);
}
How can I do that (incrementing characters value by value) in PHP ?
Variables containing characters can be incremented in PHP exactly as in C:
<?php
for ($i = 'a'; $i < 'z'; ++$i) echo $i;
Additionally, PHP allows incrementing of strings:
$x = "abc";
echo ++$x; # abd
You can also use range:
echo implode(range('a', 'z')); # abcdef....
Finally, you can convert between character and numeric ASCII index via chr and ord. respectively.
Related
Hello PHP professionals,
With:
$count = 0; // start count
$count = $count +1; // addition value
i can define an automatic counter per text paragraph.
At each paragraph the value will be incremented automatically:
1st paragraph: echo $count++;. (results is 1.) text
2nd paragraph: echo $count++;. (results is 2.) text
3rd paragraph: echo $count++;. (results is 3.) text
4th paragraph: echo $count++;. (results is 4.) text
etc.
this works without errors.
Question:
How could it be achieved that not digits 1 to x are output, but alphabetically from A to Z?
Try the following:
$letter = chr($count + 65);
If you have more than 26 paragraphs use modulo
Instead of starting with 0, you can start with 'A' or "A". The increment operator in PHP works for letters as well:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.
In your case you simply write $count = 'A'; and use echo $count++; as usual.
The chr() function transfers the passed in integer into the according ACSII character.
So you can do this:
$count = 1;
echo chr(65 + $count++); // 'A' == 65
echo chr(65 + $count++);
echo chr(65 + $count++);
It work fine, and for small letters it works with:
echo chr(97 + $count++); // 'a' == 97
Thanks for help.
How to print UFT-8 Characters from their Hexadecimal UTF-8 values? I read this post, but it did not solve my problem...
I work with many strings that are sanskrit words stored in a database. I have their HTML values, 16 bit binary code points, hex codes, and decimal codes, but I want to be able to work with their Hexadecimal UTF-8 values and output their symbolic form.
For example, here is a word आम that has a Binary UTF-8 value of 111000001010010010111000111000001010010010101110. I want to see/store/print its Hexadecimal UTF-8 value and print its symbolic form.
For example, here's a snippet of my code:
$BinaryUTF8 = "111000001010010010000110111000001010010010101110";
$Temporary = dechex(bindec($BinaryUTF8));
$HexadecimalUTF8 = NULL;
for($i = 0; $i < strlen($Temporary); $i+=2)
{
$HexadecimalUTF8 .= "\x".$Temporary[$i].$Temporary[$i+1];
}
$Test = "\xe0\xa4\x86\xe0\xa4\xae";
echo "\$Test = ".$Test;
echo "<br>";
echo "\$HexadecimalUTF8 = ".$HexadecimalUTF8;
The output is:
$Test = आम
$HexadecimalUTF8 = \xe0\xa4\x86\xe0\xa4\xae
$Test output the desired characters.
Why does $HexadecimalUTF8 not output the desired characters?
Your binary is wrong (I have fixed it below)
You are making a string containing the text "\xe0" instead of the character which represents that, The hex is just a number really.
This seems to work now
<?php
$BinaryUTF8 = "111000001010010010000110111000001010010010101110";
$Temporary = dechex(bindec($BinaryUTF8));
$HexadecimalUTF8 = NULL;
for($i = 0; $i < strlen($Temporary); $i+=2)
{
$HexadecimalUTF8 .= '\x' . $Temporary[$i].$Temporary[$i+1];
}
$Test = "\xe0\xa4\x86\xe0\xa4\xae";
echo "\$Test = ".$Test;
echo "<br>";
echo "\$HexadecimalUTF8 = " . makeCharFromHex($HexadecimalUTF8);
function makeCharFromHex($hex) {
return preg_replace_callback(
'#(\\\x[0-9A-F]{2})#i',
function ($matches) {
return chr(hexdec($matches[1]));
},
$hex
);
}
This question reminds me how poor PHP is for multi byte support
To print UTF-8 characters from their decimal value you can use this function
<?php
function chr_utf8($n,$f='C*'){
return $n<(1<<7)?chr($n):($n<1<<11?pack($f,192|$n>>6,1<<7|191&$n):
($n<(1<<16)?pack($f,224|$n>>12,1<<7|63&$n>>6,1<<7|63&$n):
($n<(1<<20|1<<16)?pack($f,240|$n>>18,1<<7|63&$n>>12,1<<7|63&$n>>6,1<<7|63&$n):'')));
}
echo chr_utf8(9405).chr_utf8(9402).chr_utf8(9409).chr_utf8(hexdec('24C1')).chr_utf8(9412);
// Output ⒽⒺⓁⓁⓄ
// Note : Use hexdec to print UTF-8 encoded characters from hexadecimal number.
For your snippet you can try this… and check it in https://eval.in/748161
<?php
// function chr_utf8 shown above is required…
$BinaryUTF8 = "111000001010010010000110111000001010010010101110";
if (preg_match_all('#(0[01]{7})|(?:110([01]{5})10([01]{6}))|(?:1110([01]{4})10([01]{6})10([01]{6}))|(?:11110([01]{3})10([01]{6}),10([01]{6})10([01]{6}))#',$BinaryUTF8,$a,PREG_SET_ORDER))
$result=implode('',array_map(function($n){return chr_utf8(bindec(implode('',array_slice($n,1))));},$a));
echo $result;
// Output आम
// Note : If you work with "binary" the length of input must be multiple of 8.
// You can't remove leading zeros because this regex will not detect the character…
One other nice inline solution is the following… (php v5.6+ required) Check it in https://eval.in/748162
<?php
$BinaryUTF8 = "111000001010010010000110111000001010010010101110";
echo pack('C*',...array_map('bindec',str_split($BinaryUTF8,8)));
// Output आम
// Note : Length or $BinaryUTF8 of input must be multiple of 8.
I want to write the algorithm for reading character by character from string in PHP. Is it possible to do this without using any builtin function...? If not possible, then can we do this by using minimum function (i.e count/size)....?
A string in PHP is in fact an array and can be addressed as such
$str = 'Hello World';
echo $str[0]; // H
echo $str[1]; // e
yes for example the string is "Hello world" then here is the code to access it
<?php
$var = "Hello World!!" ;
for($count = 0 ; $count < strlen($var) ; $count ++)
{
echo $var[$count] ;
}
?>
The above code traverses the string linearly till the value of counter is less than the string length owing to the fact that the counter was initialised to 0, Hope it helps.
today i have a problem with two strings.
The first comes from an Function-Parameter, the second string i'm getting from a file.
I only see these characters: "703866200".
When i convert the two strings in ASCII-Chars, get this (I have the characters separated by dots):
My Function-Param: 55.48.51.56.54.54.50.48.48
String from File: 55.0.48.0.51.0.56.0.54.0.54.0.50.0.48.0.48
This means that the two strings are detected always different (strcmp).
Can anybody help me? - Is it an Encoding Problem, or what means that the second String is filled with zeros?
BTW, my string2ascii function:
function string_to_ascii($string){
$ascii = NULL;
for ($i = 0; $i < strlen($string); $i++){
$ascii .= ".".ord($string[$i]);
}
return($ascii);
}
Code:
$a = "2c0";
for($i = 0; $i < 25; $i++) {
print "$a ";
$a++;
}
Output:
2c0
2c1
2c2
2c3
2c4
2c5
2c6
2c7
2c8
2c9
2d0
2d1
2d2
2d3
2d4
2d5
2d6
2d7
2d8
2d9
2e0
3
4
5
6
Why, php?
2e0 is interpreted as 2 * 10^0 (see PHP exponentiation) which is 2. The next value would therefore be 3.
From the manual:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.
Using the ++ operator on "2c0" causes PHP to increment the string value, thus "2c1". "2e0" is treated as scientific notation, i.e., 2 * 10^0 = 2, so incrementing that gives 3.
See this blog post: http://blog.rstack.cc/post/2a0_misunderstanding - it explains the problem and links the php docs for reference.