我想從輸入字段獲取值,但名稱值是最後一個。preg_match名稱最後輸入
<input value="joe" type="hidden" name="firstname">
preg_match('/input value="(.*?)" type="hidden" name="firstname"/', $request, $firstname);
這就是我正在做的,但它不工作..任何人都知道如何解決這個問題?
我想從輸入字段獲取值,但名稱值是最後一個。preg_match名稱最後輸入
<input value="joe" type="hidden" name="firstname">
preg_match('/input value="(.*?)" type="hidden" name="firstname"/', $request, $firstname);
這就是我正在做的,但它不工作..任何人都知道如何解決這個問題?
嘗試正則表達式
input(.*)?(name=\"(\w+)\")(.*)?
,並得到第三結果
你的正則表達式是優良 的preg_match的最後ARG返回數組
元素0 = entirematch
元素1 =第一個括號匹配
$request = '<input value="joe" type="hidden" name="firstname">';
if (preg_match('/input value="(.*?)" type="hidden" name="firstname"/', $request, $matches)) {
echo "MATCHED: ", $matches[1];
}
確保您的<input>
參數以源中的順序出現。請注意,例如,如果您使用螢火蟲查看它們,它們將以任意順序出現。
考慮用硬編碼空間''替換字符'
\s+
'以提高健壯性。
確定了<input …>
後,可以使用以下模式提取所有屬性(處理值分隔符(單引號,雙引號,空格))。
<?php
$input = '<input value="joe" type="hidden" name="firstname">';
$attributes = array();
$pattern = "/\s+(?<name>[a-z0-9-]+)=(((?<quotes>['\"])(?<value>.*?)\k<quotes>)|(?<value2>[^'\" ]+))/i";
if (preg_match_all($pattern, $input, $matches, PREG_SET_ORDER)) {
$attributes[$match['name']] = $match['value'] ?: $match['value2'];
}
var_dump($input, $attributes);
將導致
$attributes = array(
'value' => 'joe',
'type' => 'hidden',
'name' => 'firstname',
)