Link to home
Start Free TrialLog in
Avatar of ncw
ncw

asked on

Php preg_replace to set checkbox to checked and readonly

The code below searches for an input field with a specified id value and replaces the whole field with some text. How can I instead use similar code set a checkbox to checked and readonly? Instead of replacing the whole field I need to add these parameters; not sure if it should be:

<input type="checkbox" name="somename" id="someid" value="1" checked readonly />
or
<input type="checkbox" name="somename" id="someid" value="1" checked disabled="disabled" />
or
<input type="checkbox" name="somename" id="someid" value="1" checked="checked" disabled="disabled" />
$html = preg_replace('#<input(?=[^>]*id="some_field_id_value")[^>]*>#', 'some_text', $html);

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You can see how these form controls work here on my server:
http://www.laprbass.com/RAY_temp_ncw.php

If you can give us a little context about what you're trying to do, maybe we can help more.
<?php // RAY_temp_ncw.php
error_reporting(E_ALL);

var_dump($_GET);

$form = <<<FORM
<form>
<input type="checkbox" name="somename" id="someid" value="1" checked readonly />
<input type="checkbox" name="somename" id="someid" value="1" checked disabled="disabled" />
<input type="checkbox" name="somename" id="someid" value="1" checked="checked" disabled="disabled" />

<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

Avatar of ncw
ncw

ASKER

Hi Ray,

Both of your last 2 checkbox fields, as in the code, below seem to be cross-browser compatible, at least for FF6, IE8, Chrome, and Safari on Vista, so I think for consistency I'll run with your last option.

The other part of my question was how can I search for <input type="checkbox" name="somename" id="someid" value="1" /> in some html and replace with <input type="checkbox" name="somename" id="someid" value="1" checked="checked" disabled="disabled" /> using preg_replace. I'm actually using a function as shown below, so I need to search in the html string for an input field with specified id value and add the checked and disabled parameters just before ' />'.

I could do a str_replace but that would have to assume the type, name, id, and value properties where in a constant order.




<input type="checkbox" name="somename" id="someid" value="1" checked disabled="disabled" />
<input type="checkbox" name="somename" id="someid" value="1" checked="checked" disabled="disabled" />

function set_checkbox_readonly($search_field_id, $html, $checked = false) {
  $replace_value = ' disabled="disabled"';
  if($checked == true){
    $replace_value .= ' checked="checked"';
  }
  return preg_replace('#.....#i', $replace_value, $html);
}

Open in new window

Hmm... I guess the rules are this:

Must have <input
Followed by id=
Maybe "
-the id string-
Maybe "
followed by a group of />

Replace group with checked="checked" disabled="disabled" />

Does that sound right?

Sidebar note... Cross-browser compatibility may be enhanced if you use the HTML5 declaration like this:

<!DOCTYPE html>
<html lang="en-US">

HTML5 is a lot more permissive than Loose, Transitional or Strict.
Do me a favor and please post the test data you want me to operate on, thanks.
Avatar of ncw

ASKER

I really need the pattern to search for a string starting with '<input' and ending with '>' that contains 'id="someid"' and replace 'id="someid"' with 'id="someid" checked="checked" disabled="disabled"'.

If this can't be done in one preg_replace statement then may be I should be using preg_match first, in which case the bit I need help with please is the pattern to find the string starting with '<input', containing 'id="someid"' and ending with '>', the rest I could do.


Btw at the moment I'm using
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
One more time... Do me a favor and please post the test data you want me to operate on, thanks.  I think I understand what we want to do - I just don't want to make assumptions about the inputs.
Avatar of ncw

ASKER

There's not really anymore info to add.
One last time... Do me a favor and please post the test data you want me to operate on, thanks.  I do not mind writing the code for you, but I am busy and I don't have time to try to guess what you might have for input.
Avatar of ncw

ASKER

Ok this is not going anywhere, the data is not in a format that I can readily post. all the required info is here in the question, I only need assistance with the pattern match code thank you.
If you can't post the data you have, please make up something -- just a representative test case -- that you can post.  Or tell me where I can read a file that has something you want to process.  
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ncw

ASKER

I think I've solved it with the function below.
function tep_set_checkbox_readonly($search_field_id, $html, $checked = false) {
   $checked = ($checked)?' checked="checked"':'';
   return preg_replace('/<input([^>]+)(?!(id="'.$search_field_id.'"))([^>]+)\/>/i','<input$1 $2'.$checked.' disabled="disabled" />',$html);
}

<input type="checkbox" name="glued" id="glued" value="1" />

becomes:

<input type="checkbox" name="glued" id="glued" value="1"  checked="checked" disabled="disabled" />

Open in new window

Avatar of ncw

ASKER

Unfortunately my solution updated all checkboxes in the html.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ncw

ASKER

My comment was the solution for me.