0

I have a piece of html code as string:

<input id="an_id" name="a_name" value="some_value" class="a_class" type="text"/>

I want to get rid of the name attribute and its value with a regex and preg_replace:

preg_replace('/name\="(.*)"/', '', $html, 1);

But this outputs:

<input id="an_id" />

Can anybody help me? Why does it delete that much? The regex is name for the attribute and (.*) because I don't care what char is in the value. There can be anything.

tester
  • 3,977
  • 5
  • 39
  • 59
  • 1
    [Don't use regexes for parsing HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – John Conde Apr 04 '13 at 19:46
  • 2
    @JohnConde entire pages? Yes, bad idea. A single tag like this? Why not? – Sammitch Apr 04 '13 at 20:00

3 Answers3

1

Make it ungreedy (see Possible Modifiers):

preg_replace('/name\="(.*)"/U', '', $html, 1);

Result:

<input id="an_id"  value="some_value" class="a_class" type="text"/>

Or as @John Conde commented, use a DOM Parser.

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
1

You're not looking to match any character, you're looking to many any character other than ". Also, the capture group () is not necessary since you're not using the value in the replacement, and I don't know why you're escaping = since it's not a special character.

preg_replace('/name="[^"]*"/', '', $html, 1);
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • The escaping of = was made by preg_quote() I think. – tester Apr 04 '13 at 20:00
  • Well, the docs say it will do just that. Still weird though since `=` only ever occurs in a lookahead, but it's the third character in a 3-character sequence, `(?=...)`, and I don't see why it would ever need to be escaped... but such is the mystery of what goes on in a PHP dev's mind. – Sammitch Apr 04 '13 at 20:05
1

Primitive, but it should get you going.

<?php
function remove_attribute($attribute, $html) {
    return preg_replace(
        sprintf('~%s="[^"]+"\s?~', preg_quote($attribute)),
        null,
        $html
    );
}

$html = '<input id="an_id" name="a_name" value="some_value" class="a_class" type="text"/>';
echo remove_attribute('name', $html);
/*
    <input id="an_id" value="some_value" class="a_class" type="text"/>
*/
Anthony Sterling
  • 2,451
  • 16
  • 10