2013-02-21 114 views
4

我有一個包含像一個寫在下面的字符串變量:如何在PHP中替換字符串?

<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','no'),(381,'_transient_feed_mod_867bd5c64f85878d03a060509cd2f92c','1361425531','no'),(382,'_transient_timeout_dash_aa95765b5cc111c56d5993d476b1c2f0','1361468731','no'),(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>','no'),(384,'_transient_timeout_feed_57bc725ad6568758915363af670fd8bc','1361468732','no'),(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); 
    } 

但我沒有得到任何結果。所以如果我做了一些錯誤,請幫助。

回答

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

試試這個:

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

在變量我有「class ='class Name'」,「href ='http://ma.tt/2013/02/new-yahoo -2 /'「和」title ='Marissa Mayer在一個WordPress驅動的博客上宣佈新的雅虎版頁面。 [&hellip;]'「現在我想用class ='class Name'替換class =」class Name「,href ='http://ma.tt/2013/02/new-yahoo-2/ 'to href =「http://ma.tt/2013/02/new-yahoo-2/」and title ='Marissa Mayer在WordPress博客上發佈了新的雅虎首頁。[&hellip;]'to title =「Marissa Mayer在WordPress推出的博客上宣佈新的雅虎首頁。 [&hellip;]「 – Ranjit 2013-02-21 09:16:15

+0

我已更新,請檢查我是否有任何問題 – sanj 2013-02-21 10:07:15

0
str_replace()函數功能

if(strstr($sql, "href='")){ 

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

    $data = $sql; 
    fwrite($handle, $data); 
} 

審查文件str_replace函數的返回值

我不完全確定自己的理解是否正確,但我認爲這可能是你正在嘗試做的:

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