簡體   English   中英

固定字符串的正則表達式

[英]Regular expression for fixed string

我有以下字符串。

?page=1&sort=desc&param=5&parm2=25

我需要檢查輸入字符串url是否是嚴格格式,變量值除外。 像page =,sort =,param =,param2。

請建議正則表達式。 謝謝

您應該使用parse_str並檢查所需的所有參數是否都設置為isset 正則表達式不是去這里的方式。

也許這個:

\?page=\d+&sort=.+&param=\d+&param2=\d+

這意味着:

?page = followed by any digit repeated 1 or more times

&sort = followed by any character repeated 1 or more times

&param = followed by any digit repeated 1 or more times

&param2 = followed by any digit repeated 1 or more times

我認為Alin Purcaru的建議更好

編輯:

(\?|&)(page=[^&]+|sort=[^&]+|param=[^&]+|parm2=[^&]+)

這樣順序無關緊要

如果你關心參數的順序,就像這樣:

\?page=[^&]+&sort=[^&]+param=[^&]+param2=[^&]+$

但是Alin Purcaru是對的 - 使用已經編寫的parse_str函數來執行此操作

您可以使用以下regx / \\?page = [^&] * sort = [^&] * param = [^&] * param2 = /`來匹配:

if (preg_match("/\?page=([^&]*)sort=([^&]*)param=([^&]*)param2=([^&]*)/i", $inputstr, $matches))
{
   echo "Matches:";
   print_r($matches);     // matches will contain the params

}
else
   echo "Params nor found, or in wrong order;

正則表達式將是^\\?([\\w\\d]+=[\\w\\d]+(|&))*$只要您的值是Alpha數字,但也許您想看看過濾器是否你想驗證一個網址http://www.php.net/manual/en/book.filter.php

暫無
暫無

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

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