请问一个字符串截取的有关问题
请教一个字符串截取的问题
哪个高手有空帮我看一个函数,弄半天了不行
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?php function glstrlen($str) { $tmp_array = explode(",",$str); if(strlen($str)>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; ?>
按单词从前面截取字符串,控制在30个字符以内的长度,调试半天,不知道函数内部哪里不对,返回值为空,奇怪了,请高手帮我看看,多谢了,在线等着。
------解决方案--------------------
- PHP code
$strs = "Woods,trees,grass ,grassland,Yellow, motorcycle,highway"; $array = explode(',' , $strs); print_r(array_pad($array,30,'.')); <br><font color="#e78608">------解决方案--------------------</font><br>array_pop:将数组最后一个单元弹出(出栈),返回的是弹出的单元,并不是弹出最后一个单元后的数组。 <br><font color="#e78608">------解决方案--------------------</font><br>由于这是递归,所以你的return只是退出到上一层<br>
- 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) { // 加了个判断,如果有返回值,那么就退出,这样层层退出,调用才算结束 if(!is_null($str = glstrlen($str)) ) { return $str; } } else { return $str; } } else { return $str; } } <br><font color="#e78608">------解决方案--------------------</font><br>
- 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"); <br><font color="#e78608">------解决方案--------------------</font><br>
- 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; <div class="clear"> </div>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Vue3.2 setup syntax sugar is a compile-time syntax sugar that uses the combined API in a single file component (SFC) to solve the cumbersome setup in Vue3.0. The declared variables, functions, and content introduced by import are exposed through return, so that they can be used in Vue3.0. Problems in use 1. There is no need to return declared variables, functions and content introduced by import during use. You can use syntactic sugar //import the content introduced import{getToday}from'./utils'//variable constmsg='Hello !'//function func

JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!
