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.

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.

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.