我知道通过使用jQuery,我可以使用$('span').attr('title')
来获取属性标题的值,但我怎样才能使用PHP来做到这一点?是否有可能通过PHP得到HTML元素的属性
BEFORE
<span title="2012-11-22 15:35">6 小時前</span>
AFTER
<span>22/11/2012 15:35</span>
我知道通过使用jQuery,我可以使用$('span').attr('title')
来获取属性标题的值,但我怎样才能使用PHP来做到这一点?是否有可能通过PHP得到HTML元素的属性
BEFORE
<span title="2012-11-22 15:35">6 小時前</span>
AFTER
<span>22/11/2012 15:35</span>
您可以将其加载到DOM文档中,或使用simplexml。
实例与DOM文档:
// Load your HTML as a string here, or you can open a file (see docs)
$myHtml = new \DOMDocument();
$myHtml->loadHTML($htmlString);
// Get all span tags
$spans = $myHtml->getElementsByTagName('span');
foreach ($spans as $span) {
// get
$span->getAttribute('title');
// set
$span->setAttribute('title', 'new title');
// append text node (like innerHTML)
$span->appendChild($myHtml->createTextNode('something'));
}
要更换你需要创建一个TextNode,并追加到跨度元素的文本。
你在服务器的PHP_字符串中是否有'',或者你希望在客户端浏览器中做到这一点?(你不能)? –
为什么你需要用PHP来做到这一点?如果想使用PHP只是发送输出为你想要的' 22/11/2012 15:35' –
@MichaelBerkowski是的,我有''在字符串中,并希望在渲染到浏览器之前做到这一点。 –