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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.