使用simplexml_load_string提取属性


Extracting attribute with simplexml_load_string

我正在尝试将链接的href属性提取为字符串。

<a href="http://example.com" target="_blank" class="someclass">Read More</a>

我使用以下内容来读取提取属性:

$link = simplexml_load_string($ad['meta_value']);
$order['logo'] = $logo['href']->asXML();

我得到的不是http://example.com,而是href="http://example.com"。除了使用str_replace()之外,还有一种方法可以将属性提取为字符串吗?

@attributes视为对象并转换为字符串:

$link = simplexml_load_string($ad['meta_value']);
echo (string) $link->attributes()->href;