簡體   English   中英

使用正則表達式和preg_match_all獲取值

[英]get values using regular expression and preg_match_all

我有字符串數據

$pages = "mangaName=&authorArtist=asdas123s&genres=sn&genres[23]=on&genres[29]=on&status=&chrome=&submit=+";

function get_h1($file){
    $h1tags = preg_match_all('/\[(\w*)\]/is',$file,$patterns);
    $res = array();
    array_push($res,$patterns[2]);
    array_push($res,count($patterns[2]));
    return $res;
}

我想獲取類型[23],類型[29]的數字

[0] => 23 [1] => 29

$pages = "mangaName=&authorArtist=asdas123s&genres=sn&genres[23]=on&genres[29]=on&status=&chrome=&submit=+";

parse_str($pages, $parsed);

var_dump(array_keys($parsed['genres'])); // array(2) { [0]=> int(23) [1]=> int(29) }

zerkms的答案是正確的答案,但是如果您真的想使用regexp,請使用以下答案來更改答案:

preg_match_all('/\[(\w*?)\]/is',$file,$patterns);
 note the non-greedy __^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM