Home >Backend Development >PHP Tutorial >php intercepts characters before comma

php intercepts characters before comma

WBOY
WBOYOriginal
2016-07-29 09:06:222236browse

前置:
$foo = 'aaaaaa,vvvvvv,ccccc,dddd';
if(($index = strstr($foo,',') !== false):

答案1:用正则-》
$Regex = '#([^,]+)#is';
preg_match($Regex,$foo,$result);
echo $result[1]; //输出结果:aaaaaa
答案2:用分割字符串-》
$foo = explode(',',$foo);
echo $foo[0];  //输出结果:aaaaaa
答案3:用寻找和截取字符串-》
echo substr($foo,0,$index);   //输出结果:aaaaaa
 
后置:
:endif;

The above introduces PHP to intercept the characters before the comma, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:PHP_基础Next article:php实现的xml操作类