Jump to content

[SOLVED] Replace text within tags with formatted version


Garrett

Recommended Posts

I have a set of tags

[nohtml] [/nohtml]

On my website a user submits a form containing data in a text area. This can include html, but I want to give the user a simple option to disable html on a portion of it. This is where the nohtml tags come in.

 

For example say a user submits this:

<a href="test.php">test</a>
<br />
[nohtml]
<a href="test2.php">test2</a>
[/nohtml]

 

The regex should grab all data within (not including) the [nohtml] and [/nohtml] tags. It should format the grabbed data with htmlentities().

 

After all is done it should look like this: (test would be a hyperlink)

test

<a href="test2.php">test2</a>

 

I am unsure on how to do this all.

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

I would do something like this:

 

<?php
$str = '<a href="test.php">test</a>
<br />
[nohtml]
<a href="test2.php">test2</a>
[/nohtml]';
$str = preg_replace_callback(
'~\[nohtml\](.*?)\[nohtml\]~is',
create_function(
	'$matches',
	'return htmlentities($matches[1], ENT_QUOTES);'
),
$str
);
echo $str;
?>

 

preg_replace_callback() @ the manual.

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.