<?php $string = 'test tag <script> and <p>'; function check_tag($tag) { if (in_array(str_replace('/', '', $tag), array('p', 'b', 'hr', 'a'))) { return '<'.$tag.'>'; } return false; } echo preg_replace('/<(.*?)>/ie', 'check_tag("\\1")', $string); ?>