Forum Moderators: coopster

Message Too Old, No Replies

Using preg_replace

Adding 'tabindex' to form inputs

         

ahmedtheking

9:31 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm lazy! So using an auto accesskey writing function I made, I decided to mod it so that it would automatically add tabindex to any inputs in forms. but not to type=hidden.

Heres the code:


// same as access keys, but with tab indexes
$template = preg_replace ("/(\<input)(.*)[(type=\"input\")¦(type=\"submit\")¦(type=\"reset\")](.*)(\/\>)/", "\\1\\2 tabindex=\"{tabindex}\" \\4\\5", $template);

// now replace all {accesskey}'s with numbers!
$counter = "1";
$tbsearch = explode ("{tabindex}", $template);

foreach ($tbsearch as $key => $value) {
if (!$key > 0) {
$tbreplaced .= $value;
} else {
// add counter and the link text!
$tbreplaced .= $counter."\"".$value;
$counter++; }
}

// set the new template
$template = $tbreplaced;

Now the problem I keep getting is that...
1. it doesn't apply the changes to just type= text submit and reset (just for testing purposes!)
2. it randomly adds them in a strange place
output code > <input .... value=" tabindex=3" />

Any ideas?

ahmedtheking

10:03 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah I'm such a fool! Updated code:

// same as access keys, but with tab indexes
$template = preg_replace ("/\<input /", "<input tabindex=\"{tabindex}\" ", $template);
$template = preg_replace ("/\<textarea /", "<textarea tabindex=\"{tabindex}\" ", $template);

// now replace all {accesskey}'s with numbers!
$counter = "1";
$tbsearch = explode ("{tabindex}", $template);

foreach ($tbsearch as $key => $value) {
if (!$key > 0) {
$tbreplaced .= $value;
} else {
// add counter and the link text!
$tbreplaced .= $counter.$value;
$counter++; }
}

// set the new template
$template = $tbreplaced;