0

I want to replace a string such as:

<input type="hidden" name="id" value="12345" />

but the problem I have is that the value's value (12345) is different everytime so how can I do what I'm trying to do? ..I'm guessing regex' but haven't a clue.

2 Answers 2

6
preg_replace('/\<input type="hidden" name="id" value="[0-9]+" \/\>/is', '', $source)
Sign up to request clarification or add additional context in comments.

1 Comment

The s pattern modifier is pointless when there are no dots in the pattern.
0

Reliably and intuitively target and destroy the first occurring HTML input element with the qualifying name attribute using DomDocument and XPath.

Code: (Demo)

$html = <<<HTML
<form>
<input type="hidden" name="id" value="12345" />
</form>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$input = (new DOMXPath($dom))
    ->query('//input[@name="id"]')
    ->item(0);
$input->parentNode->removeChild($input);
echo $dom->saveHTML();

Output:

<form>

</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.