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

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

WBOY
Release: 2016-06-13 12:05:19
Original
1746 people have browsed it

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 />*/
Copy after login

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

------解决方案--------------------
$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 />            }
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template