|
chunk_split
Split a string into smaller chunks
(PHP 4, PHP 5)
Example 2398. chunk_split() example<?php Code Examples / Notes » chunk_splitsbarnum
[Editor's note: You can always use wordwrap('FF99FF', 2, ':', 2); to avoid this] chunk_split will also add the break _after_ the last occurence. So, attempting to split a color into base components, chunk_split('FF99FF', 2, ':'); will return FF:99:FF: mark
When using ssmtp for simple command line mailing: $mail_to = "destination@emailbox.com"; $msg = "this would be an actual base64_encoded gzip msg"; $date = date(r); $mail = "X-FROM: root@sender.org \n"; $mail .= "X-TO: ".$mail_to. " \n"; $mail .= "To: ".$mail_to. " \n"; $mail .= "Date: $date \n"; $mail .= "From: root@sender.org \n"; $mail .= "Subject: lifecheck \n"; $mail .= $msg." \n"; exec("echo '$mail' | /usr/sbin/ssmtp ".$mail_to); be sure to invoke chunk_split() on your message body - ssmtp becomes unhappy with long lines and will subsequently trash your message. phpkid
Well I have been having issues with a shoutbox I am coding it would keep expanding the <TD> if there were large words in it but I fixed it with this: function PadString($String){ $Exploded = explode(" ", $String); $Max_Parts = count($Exploded); $CurArray = 0; $OutString = ''; while($CurArray<=$Max_Parts) { $Peice_Size = strlen($Exploded[$CurArray]); if($Peice_Size>15) { $OutString .= chunk_split($Exploded[$CurArray], 12, " "); $CurArray++; } else { $OutString .= " ".$Exploded[$CurArray]; $CurArray++; } } return $OutString; } kevin
To phpkid: This is a much simpler solution. <?php function longWordWrap($string) { $string = str_replace("\n", "\n ", $string); // add a space after newline characters, so that 2 words only seperated by \n are not considered as 1 word $words = explode(" ", $string); // now split by space foreach ($words as $word) { $outstring .= chunk_split($word, 12, " ") . " "; } return $outstring; } ?> lehongviet
This function works well to cut long para for preview without cutting word. Good for Unicode such as ṭ function split_hjms_chars($xstr, $xlenint, $xlaststr) { $xlenint = strpos($xstr," ",$xlenint); return substr($xstr,0,$xlenint).$xlaststr; } hansvane
This function is very simple and many other functions make this on PHP 5 and even some ones in 4 the good think about this one is that work on php 3.0.6 and 4 function split_hjms_chars($xstr, $xlenint, $xlaststr) { $texttoshow = chunk_split($xstr,$xlenint,"\r\n"); $texttoshow = split("\r\n",$texttoshow); $texttoshow = $texttoshow[0].$xlaststr; return $texttoshow; } // For use echo split_hjms_chars("This is your text",6,"..."); // Will return This i... It is useful to cut long text on preview lists and if the server it's old. Hope it helps some one. Hans Svane mv@nospam
the best way to solve the problem with the last string added by chunk_split() is: <?php $string = '1234'; substr(chunk_split($string, 2, ':'), 0, -1); // will return 12:34 ?> kevin @t hyguard,com
Not quite completely obvious, but... you can un_chunk_split() by: $long_str = str_replace( "\r\n", "", $chunked_str ); 06-mar-2007 10:45
in response to "hansvane at yahoo dot com dot ar" you can do that ALOT easier: <?php function split_hjms_chars($xstr, $xlenint, $xlaststr) { return substr($xstr,0,$xlenint).$xlaststr; } ?> xamine
In reply to "adrian at zhp dot inet dot pl" digit grouping function: <?php $number = strrev(chunk_split (strrev($number), 3,' ')); //If $number is '1234567', result is '1 234 567'. ?> There is a much more simple way of doing this, by using the built-in number_format() function. <?php $number = number_format($number,2,"."," "); //This will round $number to 2 decimals, use the dot (".") //as decimal point, and the space (" ") as thousand sepparator. ?> adrian
If you need to output number formated with thousand's separator, just use it: $number = strrev(chunk_split (strrev($number), 3,' ')); If $number is '1234567', result is '1 234 567'. chris
I'm not sure what versions this also occurs in but the output of chunk_split() in PHP 5.0.4 does not match the output in other versions of PHP. In all versions of PHP I have used, apart from 5.0.4 chunk_split() adds the separator (\r\n) to the end of the string. But in PHP 5.0.4 this does not happen. This had a fairly serious impact on a library I maintain so it may also affect others who are not aware of this. harish
another way to group thousands in a number, which is much simpler, is built into PHP :) www.php.net/number_format chris
@phpkid: You can avoid such long complex code and just use some CSS stuff. Just add style="table-layout:fixed" in your <td > tag and your problem will be solved. ciao danilo
>> chunk_split will also add the break _after_ the last occurence. this should be not the problem substr(chunk_split('FF99FF', 2, ':'),0,8); will return FF:99:FF |
Change Languageaddcslashes addslashes bin2hex chop chr chunk_split convert_cyr_string convert_uudecode convert_uuencode count_chars crc32 crypt echo explode fprintf get_html_translation_table hebrev hebrevc html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode quotemeta rtrim setlocale sha1_file sha1 similar_text soundex sprintf sscanf str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split str_word_count strcasecmp strchr strcmp strcoll strcspn strip_tags stripcslashes stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk strpos strrchr strrev strripos strrpos strspn strstr strtok strtolower strtoupper strtr substr_compare substr_count substr_replace substr trim ucfirst ucwords vfprintf vprintf vsprintf wordwrap |