2013-02-21 2 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

В переменной я получил «класс =„имя класса“», «HREF =" HTTP: //ma.tt/2013/02/new-yahoo -2/'"и" title = "Marissa Mayer объявляет о выпуске нового Yahoo hompage в блоге 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/" и title =' Марисса Майер объявляет о выпуске нового Yahoo hompage в блоге на WordPress. [& hellip;] 'to title = «Марисса Майер объявляет о новом Yahoo hompage в блоге с блогами WordPress. [& hellip;] " – Ranjit

+0

Я обновил, пожалуйста, дайте мне знать, если у вас возникнут проблемы – sanj

0

Проблема заключается в том, что вы не установите возвращаемое значение str_replace() функции

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

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

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

обзора документации str_replace на http://www.php.net/manual/en/function.str-replace.php

0

Я не совсем уверен, правильно ли я понял или нет, но я думаю это может быть то, что вы пытаетесь сделать:

if(strpos($sql, "href='") !== false)){ 
    str_replace("href='", 'href="', $sql); 
$data = $sql; 
fwrite($handle, $data); 
} 
Смежные вопросы