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!

2
  • 3
    You shouldn't parse HTML with Regexp You maybe want to check DOM instead Commented Jan 2, 2013 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. Commented Jan 2, 2013 at 16:19

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

I consider "they usually choke on invalid HTML" to be a benefit.
0

This is your Solution:

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

while (strpos($nameStr, ' ') !== FALSE) {
    $nameStr = str_replace(' ', '_', $nameStr);
}
echo $nameStr;
?>

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.