regex to wrap img tag with href containg the src

后端 未结 3 1098
别跟我提以往
别跟我提以往 2021-01-27 12:38

[Edited - Sorry Bart] I\'ve looked at other answers but struggling to match this. I want to wrap an image tag where the src is the second attribute (after title) with a specific

3条回答
  •  有刺的猬
    2021-01-27 13:03

    Try this :

    $newString = preg_replace('`]*)src="\\.\\./\\.\\./images/([^"]+)"([^>])*>`','', $oldString);
    

    Limitations are :

    • It will apply the changes in things like
    • If " are replaced by ' in your img tags, you'll have to change the regexp
    • It will choke on things like 6>5

    I agree with other commenters saying regexp are bad to parse HTML. But there's almost no parsing here and the format of things to replace seems to be under control (generated by tinymce).

提交回复
热议问题