abosami Posted August 13, 2010 Share Posted August 13, 2010 Hi ,, my frineds , how r u ? I have this article in my script: <<WordPress>> widgets are the dynamic objects which eases the customization of the content on <<sidebars>> and widgetized <<footers>>. <<Widgets>> allows drag-n-drop interface in the Dashboard <<admin panel>>, for easy <<management>>. I want from the script , search about any word have <<..>> and replace it by: <a href="the word.php"> the word <a/> for examle : when the script find this word: <<Hello World>> , directly update it and replace PHP Code: <a href="hello-world.php"> Hello world </a> how can work it .. thank you very much for helping me .. Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/ Share on other sites More sharing options...
sbaker Posted August 13, 2010 Share Posted August 13, 2010 You can use regular expressions to accomplish this, as explained by the tutorials and examples on http://www.regular-expressions.info Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/#findComment-1098681 Share on other sites More sharing options...
abosami Posted August 13, 2010 Author Share Posted August 13, 2010 thank you very much .. I want a example .. plz .. I wish to help me Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/#findComment-1098682 Share on other sites More sharing options...
Alex Posted August 13, 2010 Share Posted August 13, 2010 This can be accomplished using regular expressions and preg_replace_callback. Like this: $text =<<<TEXT <<WordPress>> widgets are the dynamic objects which eases the customization of the content on <<sidebars>> and widgetized <<footers>>. <<Widgets>> allows drag-n-drop interface in the Dashboard <<admin panel>>, for easy <<management>>. TEXT; $text = preg_replace_callback( '~<<([^>]+)>>~', create_function( '$match', 'return "<a href=\"" . str_replace(" ", "-", $match[1]) . ".php\">" . $match[1] . "</a>";' ), $text ); echo $text; Or using anonymous functions if you have PHP 5.3.0+ installed: $text =<<<TEXT <<WordPress>> widgets are the dynamic objects which eases the customization of the content on <<sidebars>> and widgetized <<footers>>. <<Widgets>> allows drag-n-drop interface in the Dashboard <<admin panel>>, for easy <<management>>. TEXT; $text = preg_replace_callback( '~<<([^>]+)>>~', function($match) { return "<a href=\"" . str_replace(" ", "-", $match[1]) . ".php\">" . $match[1] . "</a>"; }, $text ); echo $text; Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/#findComment-1098684 Share on other sites More sharing options...
abosami Posted August 13, 2010 Author Share Posted August 13, 2010 wow great .. thanks alot ... but I have a question .. what does '~<<([^>]+)>>~' mean? how can use it ? Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/#findComment-1098688 Share on other sites More sharing options...
Alex Posted August 13, 2010 Share Posted August 13, 2010 That's a regular expression. That pattern essentially matches everything between << and >>. Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/#findComment-1098689 Share on other sites More sharing options...
abosami Posted August 13, 2010 Author Share Posted August 13, 2010 thank you very much .. I don't know how can thank you .. thank you very very very much maybe I need to know about the batterns .. I will google it .. Quote Link to comment https://forums.phpfreaks.com/topic/210598-how-can-find-some-of-words-and-replace-it/#findComment-1098697 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.