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

WBOY
Release: 2016-06-23 13:50:59
Original
1339 people have browsed it

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

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

$string = '111a,111';            $flag = strpos($string, ',');            if($flag !== FALSE){                $arr = explode(',', $string);                echo $arr[0];            }else{                echo $string;            }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!