tags:

views:

338

answers:

4

NOTE: I am not set on using VI, it is just the first thing that came to mind that might be able to do what I need. Feel free to suggest any other program.

I have a form with nearly 100 fields that I would like to auto-fill with PHP. I know how to do the autofill, but I would like to avoid manually adding the needed text to 100 fields.

Is there an automated way I can take the text:

<input name="riskRating" id="riskRating" type="text" />

and change it to:

<input name="riskRating" id="riskRating" type="text" value="<?php echo $data['riskRating']; ?>" />

Remember that I am wanting to do this to almost 100 fields. I am trying to avoid going to each field, pasting in the PHP code and changing the variable name manually.

I'm hoping some VI guru out there knows off the top of his/her head.

+4  A: 

:%s:\(<input name="\([^"]\+\)" id="[^"]\+" type="text" \)/>:\1value="<?php echo $data ['\2']; ?>" />:gci

That's one line. HTH.

Zsolt Botykai
I don't see how this works for more than just the specific case of "riskRating"
Mark Biek
Oh, then the problem was not specified exactly. Updating the answer.
Zsolt Botykai
"Remember that I am wanting to do this to almost 100 fields. I am trying to avoid going to each field, pasting in the PHP code and changing the variable name manually."
Mark Biek
Check my update. I'm not a PHP programmer. And you had written the expected output, and did not mention that it uses a "variable" - subexpression to be regexp specific - from the original string.
Zsolt Botykai
+4  A: 

Taking some ideas from Zsolt Botykai and Mark Biek:

:%s:<input\(.* id="\([^"]*\)".*\) />:<input \1 value="<?php echo $data['\2']; ?> />:g
Daniel James
Thank you for the answers. Y'all have saved me much annoyance.
Haabda
+2  A: 

I did it like this. I'm not sure how to escape it to work in vim though. I'll edit if I can figure it out

This is the search part of the regex:

<input (.*) id="(.*?)" (.*) />

This is the replace part:

<input \1 id="\2" \3 value="<?php echo $data['\2']; ?>" />
Mark Biek
That's better than mine. :%s:<input \(.*\) id="\([^"]*\)" \(.*\) />:<input \1 id="\2" \3 value="<?php echo $data['\2']; ?>" />:g
Daniel James
Thank you for the answers. Y'all have saved me much annoyance.
Haabda
A: 

Hello,

step 1 : search the chaine type="text" :

/type="text"

Verify that all the strings you want are caught. step 2 : Subsitute with the wanted string :

:%s//type="text" value="<?php echo $data riskrating]; ?>"/g

step 3 : Be happy !