1

I'm quite new to regular expression which I have trial and error test.

How can I replace:

<input name="token" type="text" id="test" value="">

like it will first search for the id="test" if the id exist it will set a 'variable' in value="" tags.

but if value="" has containing a varianble

ex:

<input name="token" type="text" id="test" value="token" />

it should replace the whole value="token" into a a desired variable.

preg_match('%<[^>]*(id="test"[^>]*>)([^<]*)</[^>]*>%', $html, $match);

which is it only search for HTML tags which has closing tags

ex: <div id="test"></div>

and for the value from 'input tags'. I came up with this solution which is not good.

$data['token'] = 'test';

str_replace(array_keys($data),array_values($data),$html);

can someone help me and give me some tips on regular expression on PHP. thank you.

2
  • 5
    Don't use regular expressions for this, use an HTML parser instead. Commented Nov 16, 2013 at 11:28
  • yes I know but I just want to try. Like I stated I want to try trial and error Commented Nov 16, 2013 at 11:28

1 Answer 1

1

try:

<[^>]*value="(['token'"]*)"[^>]*>


Negated char class [^>] 0 to infinite times 
[greedy] matches any character except:> The character >
value=" Literal value="
1st Capturing group (['token'"]*)
Char class ['token'"] 0 to infinite times [greedy] matches:
'token'" One of the following characters 'token'"
Sign up to request clarification or add additional context in comments.

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.