[英]How to get “name” attribute of html tag using php getStr?
我正在嘗試使用PHP getStr來獲取此HTML屬性“名稱”的內容的方法,無論如何我都無法使它正常工作,我已經進行了搜索,但是找不到任何可能有幫助的東西我。
<input id="9d6e793e-eed2-4095-860a-41ca7f89396b.subject" maxlength="50" name="9d6e793e-eed2-4095-860a-41ca7f89396b:subject" value="" tabindex="1" class="subject required field" type="text"/>
我想將此值轉換為字符串:
9d6e793e-eed2-4095-860a-41ca7f89396b:主題
我可以得到像這樣的標簽的值:
<input type="hidden" name="message" value="1442814179635.Oz1LxjnxVCMMJ0QpV0wGLx4roEA="/>
使用此代碼:
getStr($b,'name="message" value="','"');
但是我找不到找到第一個屬性名稱的方法嗎?
在PHP中使用正則表達式。 該代碼應該會有所幫助:
<?php
$str = '<input type="hidden" name="message" value="1442814179635.Oz1LxjnxVCMMJ0QpV0wGLx4roEA="/>';
//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/name="([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}
輸出:
message
檢查PHP DOMElement :: getAttribute方法。 全部在手冊中
干得好:
<?php
$html = '<input id="9d6e793e-eed2-4095-860a-41ca7f89396b.subject" maxlength="50" name="9d6e793e-eed2-4095-860a-41ca7f89396b:subject" value="" tabindex="1" class="subject required field" type="text"/>';
$doc = new DOMDocument;
$doc->loadHTML($html);
$elements = $doc->getElementsByTagName("input");
foreach($elements as $element){
echo $element->getAttribute('name');
}
?>
這部分代碼將滿足您的要求:
<?php
$b = '<input id="9d6e793e-eed2-4095-860a-41ca7f89396b.subject" maxlength="50" name="9d6e793e-eed2-4095-860a-41ca7f89396b:subject" value="" tabindex="1" class="subject required field" type="text"/>';
echo "\$b = $b\n";
$rest = substr(strstr($b,'name="'),6);
echo "\$rest = $rest\n";
$name = strstr($rest,'"',true);
echo "\$name = $name\n";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.