加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中替换字符串?

发布时间:2020-12-13 17:15:26 所属栏目:PHP教程 来源:网络整理
导读:我有一个变量,其中包含如下所示的字符串: pThe post a href="http://zed1.com/journalized/archives/2012/03/11/wordpress-3-for-business-bloggers/"WordPress 3 for Business Bloggers/a appeared first on a href="http://zed1.com/journalized"Mike Lit
我有一个变量,其中包含如下所示的字符串:

<p>The post <a href="http://zed1.com/journalized/archives/2012/03/11/wordpress-3-for-business-bloggers/">WordPress 3 for Business Bloggers</a> appeared first on <a href="http://zed1.com/journalized">Mike Little&#039;s Journalized</a>.</p>";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}s:7:"pubDate";a:1:{i:0;a:5:{s:4:"data";s:31:"Sun,10 Feb 2013 17:00:13 +0000";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}}s:32:"http://purl.org/dc/elements/1.1/";a:1:{s:7:"creator";a:1:{i:0;a:5:{s:4:"data";s:11:"Mike Little";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}}}}i:49;a:6:{s:4:"data";s:13:"






";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";s:5:"child";a:2:{s:0:"";a:5:{s:5:"title";a:1:{i:0;a:5:{s:4:"data";s:85:"WordPress.tv: Cliff Seal: Content Strategy – No one cares about your content (yet.)";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}s:4:"guid";a:1:{i:0;a:5:{s:4:"data";s:28:"http://wordpress.tv/?p=15209";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}s:4:"link";a:1:{i:0;a:5:{s:4:"data";s:95:"http://wordpress.tv/2013/02/09/cliff-seal-content-strategy-no-one-cares-about-your-content-yet/";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}s:11:"description";a:1:{i:0;a:5:{s:4:"data";s:710:"<div id="v-7wXyrfye-1" class="video-player">
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/wptv.wordpress.com/15209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/wptv.wordpress.com/15209/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=wordpress.tv&blog=5089392&post=15209&subd=wptv&ref=&feed=1" width="1" height="1" /><div><a href="http://wordpress.tv/2013/02/09/cliff-seal-content-strategy-no-one-cares-about-your-content-yet/"><img alt="Cliff Seal: Content Strategy – No one cares about your content yet" src="http://videos.videopress.com/7wXyrfye/video-93ee0cad15_std.original.jpg" width="160" height="120" /></a></div>";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}s:7:"pubDate";a:1:{i:0;a:5:{s:4:"data";s:31:"Sat,09 Feb 2013 18:29:33 +0000";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}}s:32:"http://purl.org/dc/elements/1.1/";a:1:{s:7:"creator";a:1:{i:0;a:5:{s:4:"data";s:12:"WordPress.tv";s:7:"attribs";a:0:{}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";}}}}}}}}}}}}}}}}s:4:"type";i:128;s:7:"headers";a:9:{s:6:"server";s:5:"nginx";s:4:"date";s:29:"Thu,21 Feb 2013 05:50:08 GMT";s:12:"content-type";s:8:"text/xml";s:14:"content-length";s:5:"72893";s:10:"connection";s:5:"close";s:4:"vary";s:15:"Accept-Encoding";s:13:"last-modified";s:29:"Thu,21 Feb 2013 05:30:17 GMT";s:4:"x-nc";s:11:"HIT luv 139";s:13:"accept-ranges";s:5:"bytes";}s:5:"build";s:14:"20121202164312";}','no'),(380,'_transient_timeout_feed_mod_867bd5c64f85878d03a060509cd2f92c','1361468731',(381,'_transient_feed_mod_867bd5c64f85878d03a060509cd2f92c','1361425531',(382,'_transient_timeout_dash_aa95765b5cc111c56d5993d476b1c2f0',(383,'_transient_dash_aa95765b5cc111c56d5993d476b1c2f0','<div class="rss-widget"><ul><li><a class='rsswidget' href='http://ma.tt/2013/02/new-yahoo-2/' title='Marissa Mayer announces the new Yahoo hompage,on a WordPress-powered blog. [&hellip;]'>Matt: New Yahoo</a></li><li><a class='rsswidget' href='http://ma.tt/2013/02/100-gpl/' title='Creative Market just announced that all of their WordPress Themes are now 100% GPL,meaning to list in their marketplace and reach their users your theme must provide users with the same freedoms that WordPress itself does. They have some great themes already. This is fantastic news and I’m very proud of their team for taking this bold step,and as promised  [&hellip;]'>Matt: 100% GPL</a></li><li><a class='rsswidget' href='http://wordpress.tv/2013/02/20/leslie-hancock-developing-a-distinctive-online-voice/' title=' [&hellip;]'>WordPress.tv: Leslie Hancock: Developing a Distinctive Online Voice</a></li><li><a class='rsswidget' href='http://wordpress.tv/2013/02/20/scott-basgaard-help-yourself-by-helping-others/' title=' [&hellip;]'>WordPress.tv: Scott Basgaard: Help Yourself by Helping Others</a></li><li><a class='rsswidget' href='http://feedproxy.google.com/~r/weblogtoolscollection/UXMP/~3/3tj7MIPbhOc/' title='The new default theme for WordPress 3.6,titled Twenty Thirteen,is making progress. This year’s default theme is overseen by Matt Mullenweg,and built by Joen Asmussen,Konstantin Obenland,and Lance Willett. The goal for Twenty Thirteen is to offer “a focus on blogging,and great support for post formats (which are getting attention on the backend in 3.6 a [&hellip;]'>Weblog Tools Collection: Twenty Thirteen Theme in Progress</a></li></ul></div>',(384,'_transient_timeout_feed_57bc725ad6568758915363af670fd8bc','1361468732',(385,'_transient_feed_57bc725ad6568758915363af670fd8bc','a:4:{s:5:"child";a:1:{s:0:"";a:1:{s:3:"rss";a:1:{i:0;a:6:{s:4:"data";s:3:"

";s:7:"attribs";a:1:{s:0:"";a:1:{s:7:"version";s:3:"2.0";}}s:8:"xml_base";s:0:"";s:17:"xml_base_explicit";b:0;s:8:"xml_lang";s:0:"";s:5:"child";a:1:{s:0:"";a:1:{s:7:"channel";a:1:{i:0;a:6:{s:4:"data";s:72:"

现在我想替换它中的一些词.所以我用下面的代码:

if(strstr($sql,"href='")){
        str_replace("href='",'href="',$sql);
$data = $sql;
fwrite($handle,$data);
    }

但我没有得到任何结果.如果我做错了,请帮忙.

解决方法

$sql = str_replace("href='",$sql);

试试这个:

echo preg_replace("/(class|href)='([^']*)'/",'$1="$2"',$sql);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读