Ask a question about string truncation. Which of the following experts can help me read a function if I can't get PHPcode for a long time? & lt ;? Phpfunctionglstrlen ($ str) {$ tmp_array = explode (& quot;, & quot;, $ str) ask a string truncation question.
Which of the following experts can help me read a function if I have time?
PHP code
30) { array_pop($tmp_array); $str = implode(",",$tmp_array); if(strlen($str)>30) { glstrlen($str); } else { return $str; } } else { return $str; }}$strs = "Woods,trees,grass ,grassland,Yellow, motorcycle,highway";$a = glstrlen($strs);echo $a;?>
Take a string from the front by words, control the length within 30 characters, debug for half a day, do not know what is wrong inside the function, the return value is blank, strange, please help me look, thanks for waiting online.
------ Solution --------------------
PHP code
$ Strs = "Woods, trees, grass, grassland, Yellow, motorcycle, highway"; $ array = explode (',', $ strs); print_r (array_pad ($ array, 30 ,'. '));
------ Solution --------------------
Array_pop: pops up the last unit of the array (the output stack). The Returned unit is the pop-up unit, not the array after the last unit is popped up.
------ Solution --------------------
Because this is recursion, your return only exits to the previous layer.
PHP code
Function glstrlen ($ str) {$ tmp_array = explode (",", $ str); if (strlen ($ str)> 30) {// var_dump ($ str ); array_pop ($ tmp_array); $ str = implode (",", $ tmp_array); if (strlen ($ str)> 30) {// adds a judgment, if a return value exists, exit. in this way, the call ends if (! Is_null ($ str = glstrlen ($ str) {return $ str ;}} else {return $ str ;}}
------ Solution --------------------
PHP code
Function glstrlen ($ str) {$ arr = explode (',', $ str); while (strlen ($ str)> 30) {array_pop ($ arr ); $ str = join (',', $ arr);} return $ str;} echo glstrlen ("Woods, trees, grass, grassland, Yellow, motorcycle, highway ");
------ Solution --------------------
PHP code
function glstrlen($str) { if(strlen($str)>30) { $tmp_array = explode(",",$str); array_pop($tmp_array); $str = implode(",",$tmp_array); return glstrlen($str); } return $str;}$strs = "Woods,trees,grass ,grassland,Yellow, motorcycle,highway";$a = glstrlen($strs);echo $a;