str_replace does not work properly for html content get from curl
I use curl to 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开发者_如何学Python.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.
Your search string has an embeded return in it. This will have whatever line endings your PHP file has. Your curl content probably has different line endings. Try this:
$content = str_replace("\r\n", "\n", $content);
echo str_replace('<a onclick="get_content(\'http://en.wikipedia.org\');"'."\n".'style="cursor: default;">Dojo</a> Applications','OK',$content);
精彩评论