Garrett Posted October 16, 2009 Share Posted October 16, 2009 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 https://forums.phpfreaks.com/topic/177861-solved-replace-text-within-tags-with-formatted-version/ Share on other sites More sharing options...
thebadbad Posted October 16, 2009 Share Posted October 16, 2009 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 https://forums.phpfreaks.com/topic/177861-solved-replace-text-within-tags-with-formatted-version/#findComment-937972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.