Why does the Code only show 'array' [duplicate] - php

This question already has answers here:
Why does this PHP code just echo "Array"?
(6 answers)
How can I echo or print an array in PHP?
(14 answers)
Closed 4 years ago.
Hello Guys I try to short the content of a post in a preview page. I have tried out the code and it doesn't work. It shows only an 'array' as text. Is this echo wrong or any other failures?
$word=Hello World;
$words = str_word_count($word, 1,'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ');
$len = min(50, count($words));
$first_fifty = array_slice($words, 0, $len);
echo $first_fifty;
Thanks for Help...

There are 2 mistakes:
$word=Hello World; should be $word='Hello World';
You are echoing an array; do print_r ($first_fifty); instead;
All the code:
<?php
$word='Hello World';
$words = str_word_count($word, 1,'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ');
$len = min(50, count($words));
$first_fifty = array_slice($words, 0, $len);
print_r ($first_fifty);
?>
You can echo:
<?php
echo $first_fifty[0] . ' ' . $first_fifty[1];
?>

Related

php NumberFormatter format to spelling [duplicate]

This question already has answers here:
Number to words in php using NumberFormatter class
(2 answers)
Closed 2 years ago.
hi im using php NumberFormatter in laravel
now if i want to echo number i did this code
#php
$digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
echo $digit->format(round(10.557,3));
#endphp
the result will be
but i need it to be
ten and five handred and seventy five
how can i change the format ..
thanks ..
you can try to build a function manually
function numberToText($number) {
$digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
$numberParts = explode('.', (string) round($number,3));
$formatedNumber = $digit->format($numberParts[0]);
if (isset($numberParts[1] ) {
$formatedNumber .= ' and ' . $digit->format($numberParts[1]);
}
return $formatedNumber;
}
#php
echo numberToText(10.557);
#endphp

add numbers to new lines separated by comma [duplicate]

This question already has answers here:
PHP counter increment in a while loop
(5 answers)
Closed 2 years ago.
i am having some issues with my php code. what i wish to add numbers like before every lines something like below
1. some data
2. some more data
what i have tried doing is like below but it would not multiply the numbers like i want it to, it only prints 1
$num = 1;
$tdetails = $num++. str_replace(',', '<br />', $row['travellers_details']);
Could someone please show how to achieve what i am looking for?
thanks
You need to iterate and increment num each time. You can store the elements in an array first using explode:
$num = 1;
$tdetails = explode(',', 'some data, some more data');
$str = "";
for($i = 0; $i < count($tdetails); $i++)
$str .= $num++ . ". ". $tdetails[$i] . "<br>";
echo $str;
Output:
1. some data
2. some more data
You can make use of HTML ol(ordered list) tag which will automatically do the numbering for you.
<?php
$tdetails = "<ol><li>" . implode("</li><li>", explode(",",$row['travellers_details'])) . "</li></ol>";
echo $tdetails;

PHP Increase Letter Works, Decrease Does Not [duplicate]

This question already has answers here:
Decrementing alphabetical values
(3 answers)
Closed 7 years ago.
I have the following code.
$letter = 'D';
$letter++;
echo $letter; //outputs E
$letter--;
echo $letter; //outputs E
Why isn't the subtract function working?
You should make use of ord() and chr() functions:
$letter = 'D';
$val = ord($letter);
$val++;
echo chr($val);
$val--;
echo chr($val);

PHP split a very big number [duplicate]

This question already has answers here:
What's HTML character code 8203?
(8 answers)
Closed 8 years ago.
I used a number below in my code:
$MyString = '06887558108616348​33464996​60139294';
When i'm trying to split MyString into a same pieces for example: 06887558, 10861634 so on... using substr or str_split that gives me:
06887558, 10861634, 8​3346, 4996​6, 0139294
Someone explain why this happend?!?
The Code what I have tried
$MyNewString; $n = 8; // How many you want before seperation
$MyNewString = substr($MyString,0,$n);
$i = $n;
while ($i < strlen($MyString)) {
$MyNewString .= '-'; // Seperator Character
$MyNewString .= substr($MyString,$i,$n);
$i = $i + $n;
}
echo $MyNewString;
add charset utf-8
add this code
echo '<meta charset="UTF-8">';
see the result I have tried
$new_string = implode(', ', str_split($n, 8));
echo $new_string;
If you're getting funny characters in the result, then they must have been in the original string. substr and str_split don't add anything.

PHP output string with variable? [duplicate]

This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy

Categories