您好,欢迎访问一九零五行业门户网

将字符串分成两部分解决思路

将字符串分成两部分
将一个字符串,按照开头的www 和a:将字符串分成两个字符串
$str = www.asdfsfd.com,a:xiaohua,www.baidu.com,a:huanghuang,www.360buy.com,www.huahua.com,www.huanghuang.com,a:asdds,www.dd.com;将这个字符串分成两个字符串 输出为$a=www.asdfsfd.com,www.baidu.com,www.360buy.com,www.huahua.com,www.huanghuang.com,www.dd.com;
$b =a:xiaohua,a:huanghuang,a:asdds;
php,字符串处理
------解决方案--------------------
preg_match_all(/www\.[a-z]*\.[a-z]*/is, $str, $out);
preg_match_all(/a:[a-z]*/is, $str, $out2);
------解决方案--------------------
$str_arr = explode(',',$str);
foreach($str_arr as $key=>$val){
  if(substr($val,0,3) == 'www'){
    $a_str .= $val.',';
  }else{
    $b_str .= $val.',';
  }
}
$a = substr($a_str,0,-1);
$b = substr($b_str,0,-1);
------解决方案--------------------
$str = www.asdfsfd.com,a:xiaohua,www.baidu.com,a:huanghuang,www.360buy.com,www.huahua.com,www.huanghuang.com,a:asdds,www.dd.com;
preg_match_all(/(?:(www\..*?),
------解决方案--------------------
(a\:.*?)(?=,
------解决方案--------------------
$))/,$str,$match);
$str1 = preg_replace(/,{2,}/,,,implode(,,$match[1]));
echo preg_replace(/^,/,,$str1);
echo
;
$str2 =preg_replace(/(,{2,})/,,,implode(,,$match[2]));
echo preg_replace(/^,/,,$str2);
------解决方案--------------------
$str = www.asdfsfd.com,a:xiaohua,www.baidu.com,a:huanghuang,www.360buy.com,www.huahua.com,www.huanghuang.com,a:asdds,www.dd.com;
preg_match_all('/(www[\w.]+),?(a:\w+)?/',$str,$m);
echo join(',',$m[1]);
echo '
';
echo join(',',array_diff($m[2],array('')));
------解决方案--------------------
$str = www.asdfsfd.com,a:xiaohua,www.baidu.com,a:huanghuang,www.360buy.com,www.huahua.com,www.huanghuang.com,a:asdds,www.dd.com;
preg_match_all('/(www[^,]+)
------解决方案--------------------
(a:[^,]+)/', $str, $res);
$a = join(',', array_diff($res[1], array('')));
$b = join(',', array_diff($res[2], array('')));

其它类似信息

推荐信息