tags:

views:

58

answers:

2

I got from a json feed this :

<img src = "http://produits-lemieux.com/produits/bainmoussant_hg.jpg" ></img>

i need to inject alt="" into the string

Best method to do that in php ?

+3  A: 
string = '<img src = "http://produits-lemieux.com/produits/bainmoussant_hg.jpg" ></img>';
str_replace('></img>', 'alt=""></img>', $string);

Not sure why you have </img> when you can just do: alt="" />

John Conde
alt should be IN img tag, not after..
marc-andre menard
+1  A: 

Manipulate the HTML DOM with the DOM functions ?

  $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();

You can add an attribute with the createAttribute function.

TheGrandWazoo
Seems like overkill.
Ian P
It might be overkill but was good for learning.
John Conde