Home >Backend Development >PHP Tutorial > 在线求一正则表达式.解决方案

在线求一正则表达式.解决方案

WBOY
WBOYOriginal
2016-06-13 13:39:10973browse

在线求一正则表达式.

,有这么一些字符串.

BS0001/BK0003,或者 BK00006,或者 BK00006/BBM0009/DDK0003


如果有斜杠,要返回斜杠前的内容,没有就是本身.

以上三个返回.
BS0001,BK00006,BK00006


求一正则表达来实现

------解决方案--------------------

PHP code
$s='BS0001/BK0003,或者 BK00006,或者 BK00006/BBM0009/DDK0003';
preg_match_all('#([^/\s]+?)/#s',$s,$m);
print_r($m[1]);

/*
Array
(
    [0] => BS0001
    [1] => BK00006
    [2] => BBM0009
)
*/
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code

$str    = "BS0001/BK0003,或者 BK00006,或者 BK00006/BBM0009/DDK0003";
echo    preg_replace("/.*?\b([\w]{6,})[\w\/]*/i", "\\1,", $str);

//BS0001,BK00006,BK00006, <div class="clear">
                 
              
              
        
            </div>
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