Home  >  Article  >  Backend Development  >  php trim用法求解,该如何处理

php trim用法求解,该如何处理

WBOY
WBOYOriginal
2016-06-13 13:40:01816browse

php trim用法求解
取一个ip的前三位字符串,大神帮我看下字符串'.'号为毛去不掉

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$str='192.168.175.45';
$arr = explode('.',$str);
array_pop($arr);
$output = '';
foreach ($arr as $k){
    $output .= '.'.$k; 
}
ltrim($output,'.');
print_r($output);


输出 .192.168.175

------解决方案--------------------
$output=ltrim($output,'.');
print_r($output);
------解决方案--------------------
$output = ltrim($output,'.');

但这里根本就不需要用ltrim()处理:
PHP code
$str='192.168.175.45';
$arr = explode('.',$str);
array_pop($arr);

$output = implode('.', $arr);

print_r($output);
<br><font color="#e78608">------解决方案--------------------</font><br>还可以这样写:<br>
PHP code
$str='192.168.175.45';
echo substr($str, 0, strrpos($str, '.'));
<br><font color="#e78608">------解决方案--------------------</font><br>你的源码
PHP code
$str='192.168.175.45';
$arr = explode('.',$str);
array_pop($arr);
$output = '';
foreach ($arr as $k){
    $output .= '.'.$k; 
}
$output = ltrim($output,'.');
print_r($output); <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn