简体   繁体   中英

str_replace() not working as expected

I'm trying to replace the src attribute of images in a string $content using str_replace .

The src of these images usually links to ../img/article/foo.jpg but I want each iteration to be set to img/article/foo.jpg instead.

My code is like this:

$content = str_replace('../img/article', 'img/article', $content);

This doesn't return any changes to the $content .

What am I doing wrong?

EDIT

Here is the full function to sanitize the 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 and corrects the relative path.

The content is from the TinyMCE editor, and a sample looks like this:

<p data-wr_replaced="true">Your job search has gone well. You've aced the interviews, you've worked through the technical tests, you've wowed the partners and you've finally got the offer you were looking for at the firm you've always wanted to work at.</p>
<p data-wr_replaced="true">But when you want to resign, your current workplace offers you more. More money, more holiday, more opportunity.</p>
<p data-wr_replaced="true">Now I'm not going to say what you should or shouldn't do - there are plenty of other guides giving you very clear advice out there:</p>
<p> <img src="../img/article/6a3184a558a98656e900f4aa106c387c.png" alt="" width="479" height="662" /></p>

The image is uploaded via the WYSIWYG editor and saves the relative path, which is incorrect on the page where the article ends up being displayed.

Although your code looks fine. If you still can't get it working, try preg_replace.

This will match any link that is '../' and strip it out. If you only want to do img/etc then change the part in brackets.

$content = '../img/article/ttt.jpg';
$content = preg_replace('/\.\.\/(.+)/', '$1', $content);
echo $content;

And if you want to get rid of multiple levels of '../' then:

$content = '../../img/article/ttt.jpg';
$content = preg_replace('/(\.\.\/)+(.+)/', '$2', $content);
echo $content;

Demo of the above code here.

You can use capture grouping with preg_replace function :

$content = preg_replace('/[\w/]+/(\w+/\w+)/i', '$1', $content);

Demo

I think this is what you are trying to do? Find the links in the $content (posted by users) and replace the url in images from "../" to just "/"

$content = "<h1>This is the article header</h1> <p>This is a paragraph</p> <img src='..img/article/foo.jpg' alt='this is a image which src i want to replace'/>";

$find = "..img/article";
$replace = "img/article";
$content = str_replace($find,$replace,$content);

echo $content;

http://www.tehplayground.com/#ujK2N2ldQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM