Jump to content

[SOLVED] preg_replace help


adam84

Recommended Posts

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);

 

 

Link to comment
https://forums.phpfreaks.com/topic/122332-solved-preg_replace-help/
Share on other sites

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.