lostnucleus Posted February 18, 2009 Share Posted February 18, 2009 For exaple in <input type="text" name="UserName" value="james" > I wat to take out name and vlaue values, for this is used '/<input type="[^r"]+"[^>]+name="([^"]+)"[^>]+value="([^"]*)"[^>]+>/i' which works good for double quotes But what if its single quotes insted of double ... <input type='text' name='UserName' value='james' > How can i write a single regex pattern which works on both types of quotes ?? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 18, 2009 Share Posted February 18, 2009 I wat to take out name and vlaue values $str = '<input type="text" name="UserName" value="james" >'; $str = preg_replace('#(<input.+?name=[\'"])[^\'"]+([\'"].+?value=[\'"])[^\'"]+([\'"][^>]+>)#i', '$1$2$3', $str); Output (via right click view source): <input type="text" name="" value="" > I could have encased the first set of each quotes into captures like so: ([\'"]) then [^\'"]+, and finally use back referencing to use the same exact quote that was captured using \\x format. The reason I avoided this is simply to reduce captures... in the end, the above still works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.