Question about string truncation

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.