开发者

str_replace does not work for some cases

开发者 https://www.devze.com 2023-02-25 18:25 出处:网络
I use curl to开发者_C百科 get html and save it to $content. Then I try the str_replace, it doesn\'t work:

I use curl to开发者_C百科 get html and save it to $content. Then I try the str_replace, it doesn't work:

echo str_replace('<a onclick="get_content(\'http://en.wikipedia.org\');" style="cursor: default;">Dojo</a> Applications','OK',$content);

But when I try to print $content and copy the source and save it to $content again, it works:

echo $content; Then I copy the printed and save it to $content again:

$content='It is <a onclick="get_content(\'http://en.wikipedia.org\');" style="cursor: default;">Dojo</a> Applications';

With the new $content, the replacement above works.


Try to put '\"' also. I think it's a matter of this. I don't think the "style" tag has nothing that makes the difference.


My guess is extra spaces or a newline or similar, try replacing chunks.. eg

str_replace('<a onclick="get_content(\'http://en.wikipedia.org\');" ','OK',$content);
str_replace('" style="cursor: default;">Dojo</a> Applications','OK',$content);

and try and work out where it is failing, then you can track down why


This works for me:

<?php
$content='it is <a onclick="get_content(\'http://en.wikipedia.org\');" style="cursor: default;">Dojo</a> Applications';
echo str_replace('<a onclick="get_content(\'http://en.wikipedia.org\');" style="cursor: default;">Dojo</a> Applications','OK',$content); 

So you probably have actual line feeds inside the strings and they aren't encoded in the same format, e.g., one is \n (Linux) and the other is \r\n (Windows). You can normalize both strings before comparing:

<?php
$content = strtr($content, array(
    "\r\n" => PHP_EOL,
    "\r" => PHP_EOL,
    "\n" => PHP_EOL,
));

In any case, PHP has excellent functions to handle HTML. I would not recommend regular expressions for the task: they're unreliable and very hard to get almost right.

0

精彩评论

暂无评论...
验证码 换一张
取 消