我試圖解析字符串,但我得到什麼返回不正確,我想只提取從文檔問號=?和幻燈片= ?,而是我得到PHP strpos忽略字符
文件ID: & DOC = 9729 &幻燈片= 214679 &#DOC = 9729 &幻燈片= 21
幻燈片ID: &幻燈片= 214679 &#DOC = 9729
字符串:
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;
?>
只需使用['parse_str($海峽,$變量)'](http://us.php.net/parse_str),而不是你自己發明的解析器。 – DCoder
@Dcoder由於不知道parse_str會嘗試一下 – Edward