Using PHP to Convert Decimal to ASCII using SPACE delimiters - php

Using PHP, how do you convert a string of decimal numbers with spaces in between into a string without spaces? (unless of course it is a DEC space (32) converted)
Example: 84 104 97 110 107 32 121 111 117
I have checked out the related questions and most of them are just asking what the built in function is for converting decimal to ascii. I know chr() and ord() and I think the solution really should use explode() and implode() along with a string replace. I am just horrible at for and foreach loops so the logic breaks my mind. :)
The closest SO topic I found is this one which is basically the opposite of what I am asking for -
Using PHP to Convert ASCII Character to Decimal Equivalent

This would be a situation where the strtok function actually could be used for something.
The strtok function tokenizes a string based on a character. In this case the token delimiter is a space. Each time you call strtok it returns the next token in the string.
The chr function is used to convert the ordinal (decimal) number to its ASCII character equivalent.
function myParseString($str) {
$output = ''; // What we will return
$token = strtok($str, ' '); // Initialize the tokenizer
// Loop until there are no more tokens left
while ($token !== false) {
$output .= chr($token); // Add the token to the output
$token = strtok(' '); // Advance the tokenizer, getting the next token
}
// All the tokens have been consumed, return the result!
return $output;
}
$str = '84 104 97 110 107 32 121 111 117';
echo myParseString($str);
(And you are welcome.)

Related

Count tweet length like twitter with PHP

I want to count tweet length like twitter, I try using mb_strlen and strlen all these type here
The problem is twitter count "👍🏿✌🏿️ #mention" as 15, But I get these result and I don't know how twitter count emoji and how to approach this with php
My result:
strlen: 27
mb_strlen UTF-8: 14
mb_strlen UTF-16: 13
iconv UTF-16: 14
iconv UTF-16: 27
From Twitter's developer documentation:
For programmers with experience in Unicode processing the short answer to the question is that Tweet length is measured by the number of codepoints in the NFC normalized version of the text.
So to calculate the length of a tweet in PHP, you would first normalize the text using Normalization Form C (NFC) and then count the number of codepoints (NOT CHARACTERS) in the normalized text.
$text = "👍🏿✌🏿️ #mention";
// Get the normalized text in UTF-8
$NormalizedText = Normalizer::normalize($text, Normalizer::FORM_C );
// Now we can calculate the number of codepoints in this normalized text
$it = IntlBreakIterator::createCodePointInstance();
$it->setText($NormalizedText);
$len = 0;
foreach ($it as $codePoint) {
$len++;
}
echo "Length = $len"; // Result: Length = 15
#Sherif Answer is not working in some cases.
I found this library that work perfectly nojimage/twitter-text-php
here is my code
use Twitter\Text\Parser;
$validator = Parser::create()->parseTweet($caption);
if ($validator->weightedLength > 280) {
throw new MessageException("Maximum post length is 280 characters.");
}

PHP detect variable length string contains any character other than 1

Using PHP I sometimes have strings that look like the following:
111
110
011
1111
0110012
What is the most efficient way (preferably without regex) to determine if a string contains any character other then the character 1?
Here's a one-line code solution that can be put into a conditional etc.:
strlen(str_replace('1','',$mystring))==0
It strips out the "1"s and sees if there's anything left.
User Don't Panic commented that str_replace could be replaced by trim:
strlen(trim($mystring, '1'))==0
which removes leading and trailing 1s and sees if there's anything left. This would work for the particular case in OP's request but the first option will also tell you how many non-"1" characters you have (if that information matters). Depending on implementation, trim might run slightly faster because PHP doesn't have to check any characters between the first and last non-"1" characters.
You could also use a string like a character array and iterate through from the beginning until you find a character which is not =='1' (in which case, return true) or reach the end of the array (in which case, return false).
Finally, though OP here said "preferably without regex," others open to regexes might use one:
preg_match("/[^1]/", $mystring)==1
Another way to do it:
if (base_convert($string, 2, 2) === $string) {
// $string has only 0 and 1 characters.
}
since your $string is basically a binary number, you can check it with base_convert.
How it works:
var_dump(base_convert('110', 2, 2)); // 110
var_dump(base_convert('11503', 2, 2)); // 110
var_dump(base_convert('9111111111111111111110009', 2, 2)); // 11111111111111111111000
If the returned value of base_convert is different from the input, there're something other characters, beside 0 and 1.
If you want checks if the string has only 1 characters:
if(array_sum(str_split($string)) === strlen($string)) {
// $string has only 1 characters.
}
You retrieve all the single numbers with str_split, and sum them with array_sum. If the result isn't the same as the length of the string, then you've other number in the string beside 1.
Another option is treat string like array of symbols and check for something that is not 1. If it is - break for loop:
for ($i = 0; $i < strlen($mystring); $i++) {
if ($mystring[$i] != '1') {
echo 'FOUND!';
break;
}
}

PHP function chr and ord with special chars

In PHP when I use the ord function in order to catch the ASCII code of my character I get this behavior:
ord("a") // return 97
chr(97) // return a
But when I use a special character like Œ the returns are different:
ord("Œ") // return 197
chr(197) // return �
All of my pages are encoded in utf8. This behaviour is the same for most of the special characters.
Has somebody seen this problem in the past? How can I fix it?
ord() and chr() both use the ASCII values of characters, which is a single byte encoding. Œ is not a valid character in ASCII.
You can get each byte of a multi-byte character by specifying the byte offset, as follows:
$oethel = "Œ";
$firstByte = ord($oethel[0]); // 197
$secondByte = ord($oethel[1]); // 146
Reversing the process, however, does not work, because assigning to a string byte offset converts that string to an array:
$newOethel = "";
$newOethel[0] = chr(197);
$newOethel[1] = chr(146);
echo $newOethel;
// Output is as follows:
// PHP Notice: Array to string conversion
// Array
The black diamond with a question mark is a display problem.
Review the details of black diamond in https://stackoverflow.com/a/38363567/1766831 . There are two cases; see which one fits.

how to use similar text php code in arabic

Trying to use php similar_text() with arabic, but it's not working.
However it works great with english.
<?php
$var = similar_text("ياسر","عمار","$per");
echo $var;
?>
outbot : 5
that's wrong result, it should be 2. Is there similar_text() with arabic letters?
Here's one I'm using
//from http://www.phperz.com/article/14/1029/31806.html
function mb_split_str($str) {
preg_match_all("/./u", $str, $arr);
return $arr[0];
}
//based on http://www.phperz.com/article/14/1029/31806.html, added percent
function mb_similar_text($str1, $str2, &$percent) {
$arr_1 = array_unique(mb_split_str($str1));
$arr_2 = array_unique(mb_split_str($str2));
$similarity = count($arr_2) - count(array_diff($arr_2, $arr_1));
$percent = ($similarity * 200) / (strlen($str1) + strlen($str2) );
return $similarity;
}
So
$var = mb_similar_text('عمار', 'ياسر', $per);
output: $var = 2, $per = 25
Because the Arabic text are multibyte strings normal PHP functions cannot be used (such as 'similar_text()').
echo(strlen("عمار"));
The above code outputs: 8
echo(mb_strlen("عمار", "UTF-8"));
Using the mb_strlen function with the UTF-8 encoding specified, the output is: 4 (the correct number of characters).
You can use the mb_ functions to make your own version of the similar_text function: http://php.net/manual/en/ref.mbstring.php
Just for the record and hopefully to make some help, I want to clarify the behavior of the similar_text() function when some multi-byte character strings are given (including the character strings of the Arabic.)
The function simply treats each byte of the input string as an individual character (which implies it neither supports multi-byte characters nor the Unicode.)
The byte streams of the عمار and ياسر strings are respectively represented as the following (the bytes (in the hexadecimal representation) are separated using . and, where the end of a character is reached, then a : is used instead):
06.39:06.45:06.27:06.31 <-- Byte stream for عمار
|| || || || ||
06.4A:06.27:06.33:06.31 <-- Byte stream for ياسر
As you can tell, there are five matching, and that's the reason why the function returns 5 in this case (every two hexadecimal digits represent a byte.)

PHP - strlen returning incorrect string length caused by quote

I have the following string coming from a database: Let's Get Functional
If I run this through strlen it returns 25 characters instead of the expected 20. A var dump shows that the string looks like the above (no html references etc).
If I remove the single quote strlen returns 19 characters.
Obviously the quote is returning 5 characters instead of 1 - why? How can I stop this?
Thanks!
The ASCII entity name is &apos; for ', that equals to 5 chars : ASCII Table
The problem seems to be related to the fact that your ' is evaluated, not just readed.
Try to get your strings like that :
$myString = 'Hello World!';
Not like this :
$myString = "Hello World!";
Which reads and evaluates all your string's content instead of just reading, and so interpreting your special chars as their ASCII code.
PHP Manual says : If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters
I think your strlen() function is called with parameters containing ", so it gives the evaluated strlen(), not the readed.
try this :
$countChars = strlen(utf8_decode($myString));
utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.
Take a look at this for more informations about differences between simple and double quotes.
As #deformhead already explained, it seems that your apostrophe has been converted to the HTML &apos; string. My guess would be that between getting the string out of the database and calling strlen() on it you call htmlentities() somewhere in-between.
You can also check how many characters you get from the database in your select query with CHAR_LENGTH() (MySQL).
Another issue you might consider is that strlen() does not work well for multibyte characters so if you'll be working with non-ASCII characters then you'd better use mb_strlen() with the correct encoding. This case however would not explain the difference of 5 characters in your result (strlen() counts the bytes and not characters in a string).
Hope that helps.
It can not be.
<?php
$str = "Let's Get Functional";
echo strlen($str), "\n"; // 20
Look at code output here.
how to debug?
print the ASCII code of each char:
$str = "Let's Get Functional";
$len = strlen($str);
for ($i = 0; $i < $len; $i++)
{
echo "$i\t", ord($str[$i]), "\n";
}
this is the result:
0 L 76
1 e 101
2 t 116
3 ' 39
4 s 115
5 32
6 G 71
7 e 101
8 t 116
9 32
10 F 70
11 u 117
12 n 110
13 c 99
14 t 116
15 i 105
16 o 111
17 n 110
18 a 97
19 l 108
<?php
$string = "Let's Get Functional";
echo strlen($string);
?>
This code returns 20 characters.
I have had same problem as you , may be this will help someone.
The single quote was converted to "& #39;" which was giving me incorrect result.
simply replacing the string with single quote have solved my problem.
$string = "Let's Get Functional"; //string from POST or Database
echo strlen($string); //25
$string = str_replace("'", "'",$string);
echo strlen($string); //20

Categories