PHP functions:trim middle: Difference between revisions
(→Source: version 1 of the code finished) |
(added digg tag) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<digg /> | |||
== Description == | == Description == | ||
This PHP function will reduce long strings to shorter strings, by trimming out the middle. It can be used on any string, but was inspired by the need to reduce URLs that are | This PHP function will reduce long strings to shorter strings, by trimming out the middle. It can be used on any string, but was inspired by the need to reduce URLs that are | ||
<code>http://super.really.long.urls/so/long/they/cannot/really_fit_in_one/field,then</code> | <code><nowiki>http://super.really.long.urls/so/long/they/cannot/really_fit_in_one/field,then</nowiki></code> | ||
I'd like to reduce the middle of the URL to "[...]" | I'd like to reduce the middle of the URL to "[...]" | ||
<code>http://super.really.long.urls[...]/really_fit_in_one/field,then</code> | <code><nowiki>http://super.really.long.urls[...]/really_fit_in_one/field,then</nowiki></code> | ||
The field will look a bit nicer and still have useful information. | The field will look a bit nicer and still have useful information. | ||
== | == Bugs == | ||
The requested length must longer than the string to put in the center. | |||
== Source == | == Source == | ||
Line 53: | Line 54: | ||
[[Image:My_test_URL_is_33_characters_long.png|thumb|33 char test URLs]] | [[Image:My_test_URL_is_33_characters_long.png|thumb|33 char test URLs]] | ||
[[Category:PHP code]] |
Latest revision as of 04:31, 5 November 2007
<digg />
Description
This PHP function will reduce long strings to shorter strings, by trimming out the middle. It can be used on any string, but was inspired by the need to reduce URLs that are
http://super.really.long.urls/so/long/they/cannot/really_fit_in_one/field,then
I'd like to reduce the middle of the URL to "[...]"
http://super.really.long.urls[...]/really_fit_in_one/field,then
The field will look a bit nicer and still have useful information.
Bugs
The requested length must longer than the string to put in the center.
Source
<source lang="php"> <?php //***********************************************// // // this code is freeware // //***********************************************//
/***********************************************/ // echo trim_middle("this is a long string", 10); // thi[...]ng /***********************************************/ function trim_middle($string, $len = 33, $center = "[...]") {
if(strlen($string) <= $len) { return($string); } else { // for now, I'm not going to handle the case where $center is longer than $len $num_chars_to_keep = $len - strlen($center); $num_chars_to_keep_left = ceil($num_chars_to_keep / 2); $num_chars_to_keep_right = floor($num_chars_to_keep / 2); $left_string = substr($string,0,$num_chars_to_keep_left); $right_string = substr($string,-$num_chars_to_keep_right); return $left_string . $center . $right_string; } } ?> </source>
Why 33?
Because the URLs in our test system that *I* made happened to be 33 characters long