[英]With PHP, how can I replace <a href=“xxx”> tags with <span data-href=“xxx”> tags?
[英]Using simplehtmldom in PHP how do I get the data-href attribute?
我正在使用此PHP庫與dom一起使用: http : //simplehtmldom.sourceforge.net/
我想訪問此頁面上li元素的data-href元素: http : //www.spareroom.co.uk/flatshare/bristol/
根據api參考: http : //simplehtmldom.sourceforge.net/manual_api.htm
只要$ res代表li dom節點,此代碼就應該起作用-在我的情況下,它可以:
echo $res->data-href;
但是,當我運行時,回聲是“ 0” ....當我期望看到類似的東西時:
"/flatshare/fad_click.pl?fad_id=3248085&search_id=&offset=0&city_id=&flatshare_type=offered&search_results=%2Fflatshare%2Fbristol%2F&"
有人可以幫我了解我做錯了什么嗎
$res->data-href
是作為解析
$res->data - href
也就是說,它是一個減法,因為-
是不是在一個標識符中的有效字符。 嘗試:
$res->{"data-href"}
由於您正在訪問對象,因此必須將帶有特殊字符的鍵引號引起來並用{}引起來。 所以:
echo $res->data-herf
應該:
echo $res->{"data-href"}
老實說,盡管使用該方法可能更容易(更安全):
$res->getAttribute("data-href");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.