adam84 Posted September 2, 2008 Share Posted September 2, 2008 I am trying to create a simple wiki application on my site. But I keep on running into some problems. What I want to do is search the user inputted text for anything like this: _this will become bold_ Then I want to change it to: <b>this will become bold</b> I have never used preg_replace and I am just starting off with the whole regex thing. Any help?? This is what I have $pattern = "/^(_)[]*(_)$/"; echo preg_replace( $pattern, "<b>$text</b>", $text); Quote Link to comment https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/ Share on other sites More sharing options...
JasonLewis Posted September 2, 2008 Share Posted September 2, 2008 My knowledge of Regex isn't very good either, but you can give this a shot: $str = "_this will become bold_"; echo preg_replace("#_(.*?)_#si", "<b>\\1</b>", $str); Quote Link to comment https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/#findComment-631689 Share on other sites More sharing options...
nrg_alpha Posted September 2, 2008 Share Posted September 2, 2008 You can also resort to 'conditionals' as such: $str = '_see if I can_'; $pattern = "/^(_)?([^_]+)(?(1)_)$/"; $str = preg_replace( $pattern, '<strong>$2</strong>', $str); echo $str; If there is an underscore on both sides, they will both be replaced. If there is only one underscore on either side, there is no replacements / bold text done. (I prefer to use <strong> tags if I'm using XHTML (but I think you need b tags if you use HTML 4 if I'm not mistaken). Quote Link to comment https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/#findComment-631852 Share on other sites More sharing options...
adam84 Posted September 2, 2008 Author Share Posted September 2, 2008 Awesome thanks for your help!!!! Quote Link to comment https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/#findComment-631915 Share on other sites More sharing options...
effigy Posted September 2, 2008 Share Posted September 2, 2008 You can also resort to 'conditionals' Why would you use such complexity for a simple problem? Quote Link to comment https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/#findComment-631961 Share on other sites More sharing options...
nrg_alpha Posted September 2, 2008 Share Posted September 2, 2008 I realise it's more than needed to be sure (the conditionals are not needed at all admittedly). More for musings I suppose. Quote Link to comment https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/#findComment-632000 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.