假设我有这个HTML,我不能对它执行regex来查找值(至少我认为是这样),因为它有新行,但我需要搜索类似name="hf" value="(.*?)"
的内容。
HTML响应
<input type="hidden"
name="hf"
value="123">
当我尝试执行$response = str_replace(''r'n', $response, '');
或$response = str_replace(''n', $response, '');
时,$response
变为空字符串。我有什么选择?
好的,首先,您将参数以错误的顺序传递给str_replace
。
str_replace($search, $replace, $subject)
您的主题是",您正在用您的回复替换"''n"(不存在)。所以结果是什么都没有。
其次,"''n"不会给您换行符。您需要使用双引号来处理转义符。
str_replace("'n", '', $response);
这将修复您的原始代码。
最后,您应该使用DOMDocument
来处理HTML,而不是regex。养成正确做事的(好)习惯,从长远来看,这会为你节省时间和麻烦。
如何用PHP解析和处理HTML?在这个问题上非常全面。
获取A元素的href属性也提供了一些nie代码示例。
强烈建议使用DOM解析而不是容易出错的regex来获得这种HTML解析的解决方案。
以下是基于DOM的代码,可以用来提取输入项的值:
$html = <<< EOF
<input type="hidden"
name="hf"
value="123">
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
echo $doc->saveHTML();
$xpath = new DOMXPath($doc);
// returns a list of all inputs with name='hf'
$nodelist = $xpath->query("//input[@name='hf']");
for($i=0; $i < $nodelist->length; $i++) {
$node = $nodelist->item($i);
$value = $node->attributes->getNamedItem('value')->nodeValue;
var_dump($value); // prints "123"
}
正则表达式有可以使用的修饰符-有"m"answers"s"告诉它将其解析为多行字符串或单行。第二个可能是你喜欢的更好的选择:
preg_match('/name="hf" value="(.*?)"/s',$htmlString,$matches);