Pregunta

i want to replace all my images (with an own special attribute)

<img width="720" height="321" dl="text-text-text-text" alt="Marl's Palace of Sin" src="/images/sin-ambiente.jpg" title="Innenansichten des Hauses">

into this

<dl class="imgLeft">
  <dt>
     <img width="720" height="321" alt="Marl's Palace of Sin" src="/images/sin-ambiente.jpg" title="Innenansichten des Hauses">
 </dt>
  <dd>text-text-text-text</dd>
</dl>

my php function

function dlSytler($htmlContent)
{
    $regex = '#<img([^>]*).*dl="([^"/]*/?[^".]*\.[^"]*)"([^>]*).*>#'; 
    $replace = '<dl class="imgLeft"><dt><img$1 $3> </dt> <dd>$2</dd> </dl>';
    $htmlContent = preg_replace($regex, $replace, $htmlContent); 
    return $htmlContent;
}

the problem is, that the replace doesnt work an every image (and i dont know why).

I think my regex-rule is not perfect. Can anybody help me?

¿Fue útil?

Solución

You ask where the problem is and I'll try to give you some pointers.

For starters, the idea of [^>]*.* makes no sense. It's saying something like "match X specifically or nothing, or match anything or nothing." In that case, it would be easy to just match anything.

This is simpler.

Search: (<img.*?)(dl=.*?)(alt=[^>]*>)

Replace: <dl class="imgLeft"><dt>\1\3</dt><dd>\2</dd></dl>

Demo

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top