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

LVL 1
ncwAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ncwConnect With a Mentor Author Commented:
Final solution I found on the web and adapted as below.
function tep_set_checkbox_readonly($search_field_id, $html, $checked = false) {
   $checked = ($checked)?' checked="checked"':'';
   $html = preg_replace('|(<input*[^>]*id=)"['.$search_field_id.']+"([^>]*>)|Ui', '\1"'.$search_field_id.'"'.$checked.' disabled="disabled"\2', $html);
   return $html;
}

Open in new window

0
 
Ray PaseurCommented:
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

0
 
ncwAuthor Commented:
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

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
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.
0
 
Ray PaseurCommented:
Do me a favor and please post the test data you want me to operate on, thanks.
0
 
ncwAuthor Commented:
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">
0
 
Ray PaseurCommented:
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.
0
 
ncwAuthor Commented:
There's not really anymore info to add.
0
 
Ray PaseurCommented:
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.
0
 
ncwAuthor Commented:
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.
0
 
Ray PaseurCommented:
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.  
0
 
Ray PaseurConnect With a Mentor Commented:
http://www.laprbass.com/RAY_temp_ncw.php

Good luck with your project, ~Ray
<?php // RAY_temp_ncw.php
error_reporting(E_ALL);
echo "<pre>";



$form = <<<FORM
<form>
<input type="checkbox" name="somename" id="someid" value="1" />
<input type="submit" />
</form>
FORM;

// THE ID ATTRIBUTE
$id = 'someid';

// PREPARE THE STRING FOR USE IN A REGEX
$id = preg_quote($id);



$regex
= '#'        // REGEX DELIMITER
. '('        // START A GROUP
. '\<input ' // INPUT TAG
. '.*?'      // SOMETHING OR NOTHING
. ')'        // END OF FIRST GROUP
. '('        // START A GROUP
. 'id="'     // THE ID ATTRIBUTE
. $id        // THE ID VALUE
. '"'        // THE END QUOTE
. ')'        // THE END OF THE GROUP
. '#'        // REGEX DELIMITER
;

$string
= '${1}${2}' // THE GROUPS STAY IN THE OUTPUT STRING
. ' checked="checked" disabled="disabled" '
;


// SHOW THE ORIGINAL
echo htmlentities($form);

// MAKE THE REPLACEMENT
$form = preg_replace($regex, $string, $form);

// SHOW THE WORK PRODUCT
echo htmlentities($form);

Open in new window

0
 
ncwAuthor Commented:
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

0
 
ncwAuthor Commented:
Unfortunately my solution updated all checkboxes in the html.
0
 
ncwAuthor Commented:
My comment was the solution for me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.