douzhangcuo2174 2014-05-15 02:37
浏览 42
已采纳

将selfmade image-dl-attribute替换为正确的图像dl标记

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?

  • 写回答

1条回答 默认 最新

  • dongyue1988 2014-05-15 04:24
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?