我從一個JSON提要得到這個:PHP字符串添加標籤(修改)
<img src = "http://produits-lemieux.com/produits/bainmoussant_hg.jpg" ></img>
我需要注入alt=""
到字符串
做,在PHP的最佳方法是什麼?
我從一個JSON提要得到這個:PHP字符串添加標籤(修改)
<img src = "http://produits-lemieux.com/produits/bainmoussant_hg.jpg" ></img>
我需要注入alt=""
到字符串
做,在PHP的最佳方法是什麼?
string = '<img src = "http://produits-lemieux.com/produits/bainmoussant_hg.jpg" ></img>';
str_replace('></img>', 'alt=""></img>', $string);
不知道爲什麼你有</img>
時,你可以這樣做:alt="" />
操縱與DOM functions的HTML DOM?
$doc = new DOMDocument();
$doc->loadHTML("<html><body>Test<br></body></html>");
$params = $doc->getElementsByTagName('img'); // Find Sections
foreach($params as $param)
{
$attribute = $doc->createAttribute('alt');
$param->appendChild($root_attr1);
$attributeText = $doc->createTextNode('This is the ALT attribute');
$attribute->appendChild($root_text);
}
$doc->saveHTML();
您可以使用createAttribute函數添加屬性。
似乎過度殺傷。 – 2010-02-05 15:34:46
這可能是矯枉過正,但對學習很有幫助。 – 2010-02-05 16:34:22
ALT應該是IN IMG標記,而不是在.. – menardmam 2010-02-05 15:48:37