http://regexr.com?35hk2PHP正則表達式幫助的preg_match
上述網站顯示正確的正則表達式,但是當我用PHP做它,它不顯示某些名稱,如'JJ5x5's White Top Hat'
這裏是PHP:
<?php
function newEcho($Value){
echo $Value . "<br>";
};
function cURLAuto($URL){
$Channel = curl_init();
curl_setopt($Channel, CURLOPT_URL, $URL);
curl_setopt($Channel, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($Channel);
};
function autoMatchAll($String,$Pattern){
$Found = array();
$Match = preg_match_all($Pattern,$String,$Found);
return $Found;
};
function replaceMatch($String,$Pattern,$Subject){
return str_replace($Pattern,$Subject,$String);
};
$Count = 0;
$Output = cURLAuto("www.roblox.com/catalog/json?Subcategory=2&SortType=0&SortAggregation=3&SortCurrency=0&LegendExpanded=true&Category=2&PageNumber=1");
$AssetId = autoMatchAll($Output,'/"AssetId":[\d]+/');
$Name = autoMatchAll($Output,'/"Name":"[\w\s\d\-' . "\'" . ']+"/');
foreach($AssetId[0] as $Value){
newEcho(replaceMatch($Value,'"AssetId":',"") . ":" . replaceMatch(replaceMatch($Name[0][$Count],'"Name":"',""),'"',""));
$Count++;
};
echo $Output
?>
$Name
是我正在使用正則表達式的問題,因爲它顯示運行代碼時只顯示一些名稱。對於$Name
的正則表達式是
/"Name":"[\w\s\d\-\']+"/
但由於我不能使用「或」字符串,我不得不讓
'/"Name":"[\w\s\d\-' . "\'" . "]+/"
但是你可以幫我這個。我想解決這個問題
不需要拆分字符串,你可以使用'\''。此外,'-'屬於課程的開始部分(未轉義)。 –
@WalterTross但是我仍然有問題,因爲一些項目的名字沒有顯示出來!比如JJ5x5的白色禮帽 – wateraura