1

I am trying to redo some forms that have uppercase field names and spaces, there are hundreds of fields and 50 + forms... I decided to try to write a PHP script that parses through the HTML of the form.

So now I have a textarea that I will post the html into and I want to change all the field names from

name="Here is a form field name"

to

name="here_is_a_form_field_name"

How in one command could I parse through and change it so all in the name tags would be lowercase and spaces replace with underscores

I am assuming preg_replace with an expression?

Thanks!

Manse
  • 37,765
  • 10
  • 83
  • 108
Greg Alexander
  • 1,192
  • 3
  • 12
  • 25
  • 3
    [You shouldn't parse HTML with Regexp](http://stackoverflow.com/a/1732454/1607098) You maybe want to check [DOM](http://php.net/dom) instead – Touki Jan 02 '13 at 16:16
  • I wouldn't have put either PHP or regex at the top of the list of "best tools for the job". Some sort of DOM parser would seem to be a better bet (PHP does have one, but other languages' DOM parsers are easier to work with). It can probably be done with PHP/regex, but how complex a regex you need would depend on what else the HTML contains (in particular, whether anything other than form fields have a `name` attribute, and if so whether you'd want to process them as well). Finally, it would help if you showed us what you've tried already so we can help you build on that. – SDC Jan 02 '13 at 16:19

3 Answers3

8

I would suggest not using regex for manipulation of HTML .. I would use DOMDocument instead, something like the following

$dom = new DOMDocument();
$dom->loadHTMLFile('filename.html');

// loop each textarea
foreach ($dom->getElementsByTagName('textarea') as $item) {

    // setup new values ie lowercase and replacing space with underscore
    $newval = $item->getAttribute('name');
    $newval = str_replace(' ','_',$newval);
    $newval = strtolower($newval);
    // change attribute
    $item->setAttribute('name', $newval);
}
// save the document
$dom->saveHTML();

An alternative would be to use something like Simple HTML DOM Parser for the job - there are some good examples on the linked site

Community
  • 1
  • 1
Manse
  • 37,765
  • 10
  • 83
  • 108
1

I agree that preg_replace() or rather preg_replace_callback() is the right tool for the job, here's an example of how to use it for your task:

preg_replace_callback('/ name="[^"]"/', function ($matches) {
  return str_replace(' ', '_', strtolower($matches[0]))
}, $file_contents);

You should, however, check the results afterwards using a diff tool and fine-tune the pattern if necessary.

The reason why I would recommend against a DOM parser is that they usually choke on invalid HTML or files that contain for example tags for templating engines.

AndreKR
  • 32,613
  • 18
  • 106
  • 168
0

This is your Solution:

<?php
$nameStr = "Here is a form field name";

while (strpos($nameStr, ' ') !== FALSE) {
    $nameStr = str_replace(' ', '_', $nameStr);
}
echo $nameStr;
?>
Dan Short
  • 9,598
  • 2
  • 28
  • 53
Pawan Thakur
  • 591
  • 8
  • 19