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

php如何截取逗号之前的字符

WBOY
WBOYOriginal
2016-06-23 13:50:591397browse

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


回复讨论(解决方案)

$email   =  'name@example.com' ;
$domain  =  strstr ( $email ,  '@' );
echo  $domain ;  // 打印 @example.com

$user  =  strstr ( $email ,  '@' ,  true );  // 从 PHP 5.3.0 起
echo  $user ;  // 打印 name

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

$s = 'aaaaa,bbbbbbb,ccccc';echo strtok($s, ',');

$string = '111a,111';            $flag = strpos($string, ',');            if($flag !== FALSE){                $arr = explode(',', $string);                echo $arr[0];            }else{                echo $string;            }

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