我有一个包含手机号码的html源代码,我想从该源代码中提取电话号码,每个电话号码都有开始和结束标志,让我们说 示例HTML代码是, 每个手机号码都来自' phone ='并以%结尾,如下所示,
b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189"
我怎样才能使用php提取电话号码?
答案 0 :(得分:0)
试试这个
<?php
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = 'b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189"';
$phone = get_string_between($fullstring, '&phone=', '%');
echo $phone;
<强> Demo 强>
如果是多个
<?php
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
$str = 'b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189"<style="color:red">b2e1d163b0b4dc6ebfa5&t=s&phone=111111111111111%40cb2e1d163b0b4dc6ebfa5&t=s&phone=222222222222%40c.us&i=1522996189".us&i=1522996189"';
$str_arr = getInbetweenStrings('&phone=', '%', $str);
print_r($str_arr);
<强> Demo for Multiple 强>
答案 1 :(得分:0)
这是一种农民的方式,但它应该做你的工作:
$string = "b2e1d163b0b4dc6ebfa5&t=s&phone=95355036019918%40c.us&i=1522996189";
$explode = explode('&phone=',$string);
$phone = explode('%', $explode[1]);
var_dump($phone[0]);