我正在尝试解析一个字符串,但我得到的回复是不正确的,我试图从doc =中提取问号?和slide =?,但我得到了
doc id:& doc = 9729& slide = 214679& #doc = 9729& slide = 21
幻灯片ID:& slide = 214679& #doc = 9729
string:
HTTP:/securefinder.com/ajax_query/delimiter.aspx Q = LNG&安培; F = 1.1&安培; DOC = 9729&安培;滑动= 214679&安培;#DOC = 9729&安培;滑动= 214679&安培;
似乎它不接受&作为划界人物。
<?php
function from_to($string, $from, $to) {
//Calculate where each substring is found inside of $string
$pos_from = strpos($string, $from);
$pos_to = strpos($string, $to);
//The function will break if $to appears before $from, throw an exception.
if ($pos_from > $pos_to) {
}
return substr(
$string,
$pos_from, //From where the $from starts (first character of $from)
$pos_to - $pos_from + strlen($to) //To where the $to ends. (last character of $to)
);
}
$str = "http:/securefinder.com/ajax_query/delimiter.aspx?q=LNG&f=1.1&doc=9729&slide=214679&#doc=9729&slide=214679&";
$doc_id = from_to($str, 'doc=', '&');
$slide_id = from_to($str, 'slide=', '&');
echo 'doc id:' . $doc_id ;
echo 'slide id:'. $slide_id;
?>
答案 0 :(得分:1)
考虑使用prase_url()
分解网址,然后在查询结果上使用parse_str()
首先按#
分解您的查询,&
的结果,最后是=
的结果。
这样您就不需要编写自己的解析逻辑了。从这里你将获得一个很好的数组来操纵,而不是试图弄清楚如何操纵你的字符串。
答案 1 :(得分:0)
尝试使用 preg_match
$str = "http:/securefinder.com/ajax_query/delimiter.aspx?q=LNG&f=1.1&doc=9729&slide=214679&#doc=9729&slide=214679&";
preg_match("/doc=(.*?)&/i", $str, $d);
preg_match("/slide=(.*?)&/i", $str, $s);
$doc_id = $d[1];
$slide_id = $s[1];
echo 'doc id:' . $doc_id ;
echo 'slide id:'. $slide_id;