Take the 2-minute tour ×

I'm trying to remove all hidden tags (and the ending tag) via regular expression and it seems to work but with one problem. It leaves behind "<>" for all the elements found.

I'm using this to replace my hidden fields with blank:

$saveContent = preg_replace('<input type="hidden" .*? />', "", $saveContent);
$saveContent = preg_replace('</form>', "", $saveContent);

It just brings back "<><><>" (2 Hidden fields and the ending form tag). I tried to string replace <> and that doesn't seem to work either

Am I missing something?

share|improve this question
Whenever you try to parse HTML with regex, you make a kitten cry. stackoverflow.com/questions/1732348/… –  GordonM Mar 22 '12 at 15:16
add comment

5 Answers

The problem (except that of trying to match HTML with regex) is that you don't properly quote the expression inside the string, which is usually done in PHP like "/regex/", but any character can be used instead of the slashes, eg "~regex~".

In your case < is the quoting character, which makes the end quote > (i.e "<regex>"), thus making it valid in preg_* and not giving you any errors.

For example:

preg_replace('</form>', "", $str)

is the same as

preg_replace('~/form~', "", $str)


preg_replace('/\/form/', "", $str)

All of which replace /form with an empty string.

While you wanted:

preg_replace('~</form>~', "", $str)
share|improve this answer
Say rather that < and > are the regex delimiters. "Quoting character" could just as easily refer to the quotes (" or ') surrounding the whole regex, or to the escape character, \. –  Alan Moore Mar 22 '12 at 19:57
add comment

You need to escape slashes.. and add slashes for modifiers to work http://php.net/manual/en/reference.pcre.pattern.modifiers.php

$saveContent = preg_replace('/<input type="hidden" .*? \/>/i', "", $saveContent);    
$saveContent = preg_replace('/<\/form>/i', "", $saveContent);
share|improve this answer
That doesn't seem to do anything. It leaves the form tag there. –  adamzwakk Mar 22 '12 at 14:56
Try replacing >/i with simply >/. –  Robin Castlin Mar 22 '12 at 15:02
How is making it case-sensitive help? –  Artjom Kurapov Mar 22 '12 at 15:03
Wops I though reversed. Forgot that /i was the case insensitive version, sorry. –  Robin Castlin Mar 22 '12 at 15:05
add comment

If I remember correctly you can put the pattern between %'s as to avoid all that escaping which makes practical unreadable, for example

if (preg_match('%</form>%', $subject)) {
    # Successful match
} else {
    # Match attempt failed
share|improve this answer
No dice, it doesn't find </form> even though it's there. –  adamzwakk Mar 22 '12 at 15:11
that's just weird :) If its literally there I'm thinking that it's an encoding issue of some sort? Try simpler forms like a match and just "form" then add <, then > and so on? –  buckley Mar 22 '12 at 15:27
add comment
up vote 0 down vote accepted

Turns out for some reason (that I wasn't aware of), the < and > symbols were being converted to entities, but only for a select few.

I just checked for those entities and string replaced them to the correct symbols and it worked.

share|improve this answer
The regexes in your question are still incorrect, as @Qtax explained. Did you also add regex delimiters, as in '~</form>~' ? –  Alan Moore Mar 22 '12 at 20:02
add comment

Try this

$content = '<input type="hidden" name="abc" /> abc <input type="hidden" name="abc" />';
preg_replace('#<input type="hidden"[^>]+>#', '', $content);
share|improve this answer
add comment

Your Answer


