php 提取字符串中指定内容及将手机号内容替换

1、提取字符串中指定之间的内容,并将手机号、身份证等敏感信息替换为******:

PHP程序:

';
if(preg_match('/(.*?)<\/msg>/i', $str, $matches)) {
   print_r($matches);
   echo '我要截取的内容:' . $matches[1] . "\n";
  $content = preg_replace('/([0-9]{11,})|([0-9]{3,4}-[0-9]{7,10})|([0-9]{3,4}-[0-9]{2,5}-[0-9]{2,5})/', '******', $matches[0]);
}

echo '替换后的内容为: ' . $content; 


?>

执行php脚本:/opt/lampp/bin/php mat.php 

输出:

Array
(
    [0] =>
    [1] =>
)

我要截取的内容:


替换后的内容为:


2、另一种提取指定字符串的方法:截取之间的字符串内容

';
//每一次查到start -end;  
$start1 = strpos($str,'',0);
$end1 = strpos($str,'',0);
echo('截取的字符串为: ' .substr($str, $start1, $end1));

?>

输出:

/opt/lampp/bin/php match_test.php 
截取的字符串为:  





你可能感兴趣的:(php开发,shell)