2016-06-30 22 views
1

輸入字符串是:simplexml的手柄報價爲實體

<input type="hidden" value="CZĘŚCI"" name="userlogin">

<input type="hidden" value="CZĘŚCI'" name="userlogin">

如果我嘗試用simplexml_load_string這個回報解析錯誤處理這個問題。

警告:simplexml_load_string():實體:行1:解析錯誤

我知道,這是不正確的HTML和我應該使用htmlspecialchars或等爲功能sanitaze HTML,但串到我這裏來的一個外部來源,我無法控制它。我需要解析錯誤的HTML。

如何處理此問題並獲取此元素的值?

+0

simplexml_load_string($ XMLDATA '的SimpleXMLElement',LIBXML_NOCDATA | LIBXML_NOBLANKS); –

+0

在這裏刪除第二個關閉引用 - 'value =「CZĘŚCI」「'並添加根項目 - https://eval.in/598358 – splash58

+0

Manish Jesani,它返回false; splash58 - 我不能這樣做,因爲輸入字符串可能是不同的html。 – stdex

回答

0

SimpleXml只能解析有效的XML。您正試圖解析無效的HTML。

您可以使用DOM實現你想要什麼:

$string = <<< HTML 
<input type="hidden" value="CZĘŚCI'" name="userlogin"> 
HTML; 

libxml_use_internal_errors(true); 
$dom = new DOMDocument; 
$dom->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . $string); 
echo $dom->getElementsByTagName('input')->item(0)->getAttribute("value"); 
libxml_use_internal_errors(false);