Home >Backend Development >PHP Tutorial >php怎么截取逗号之前的字符

php怎么截取逗号之前的字符

WBOY
WBOYOriginal
2016-06-13 12:05:191740browse

php如何截取逗号之前的字符
aaaaa,bbbbbbb,ccccc  
首先判断是否有逗号存在(不知道这一步时候需要,因为有的存在没有逗号的情况),如果存在截取第一个逗号前面的字符(不包涵逗号)
------解决方案--------------------

$str='aaaaa,bbbbbbb,ccccc';<br />if(strpos($str,',')){<br />	preg_match('/([^,]+?),/i',$str,$match);<br />}<br /><br />print_r($match[1]);<br />/*<br />aaaaa<br />*/

------解决方案--------------------
$s = 'aaaaa,bbbbbbb,ccccc';<br />echo strtok($s, ',');

------解决方案--------------------
$string = '111a,111';<br />            $flag = strpos($string, ',');<br />            if($flag !== FALSE){<br />                $arr = explode(',', $string);<br />                echo $arr[0];<br />            }else{<br />                echo $string;<br />            }

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