php - str_replace() not working as expected -
i'm trying replace src
attribute of images in string $content
using str_replace
.
the src
of these images links ../img/article/foo.jpg
want each iteration set img/article/foo.jpg
instead.
my code this:
$content = str_replace('../img/article', 'img/article', $content);
this doesn't return changes $content
.
what doing wrong?
edit
here full function sanitize user's input:
function remove_styles($text) { $content = trim($content); $content = preg_replace('#(<[a-z ]*)(style=("|\')(.*?)("|\'))([a-z ]*>)#', '\\1\\6', $text); $content = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $content); $content = strip_tags($content,'<h2><h3><p><strong><a><ul><li><img>'); $content = str_replace('../img/article','img/article',$content); return $content; }
this removes styles, trims whitespace, strips tags , corrects relative path.
the content tinymce editor, , sample looks this:
<p data-wr_replaced="true">your job search has gone well. you've aced interviews, you've worked through technical tests, you've wowed partners , you've got offer looking @ firm you've wanted work at.</p> <p data-wr_replaced="true">but when want resign, current workplace offers you more. more money, more holiday, more opportunity.</p> <p data-wr_replaced="true">now i'm not going should or shouldn't - there plenty of other guides giving clear advice out there:</p> <p> <img src="../img/article/6a3184a558a98656e900f4aa106c387c.png" alt="" width="479" height="662" /></p>
the image uploaded via wysiwyg editor , saves relative path, incorrect on page article ends being displayed.
you can use capture grouping preg_replace
function :
$content = preg_replace('/[\w/]+/(\w+/\w+)/i', '$1', $content);
Comments
Post a Comment