Solved

Php preg_replace to set checkbox to checked and readonly

Posted on 2011-09-15
15
518 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ncw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36544318
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
 
LVL 1

Author Comment

by:ncw
ID: 36544547
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36544742
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36544845
Do me a favor and please post the test data you want me to operate on, thanks.
0
 
LVL 1

Author Comment

by:ncw
ID: 36544914
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36545541
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
 
LVL 1

Author Comment

by:ncw
ID: 36546095
There's not really anymore info to add.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36546142
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
 
LVL 1

Author Comment

by:ncw
ID: 36546221
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36546260
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 36546345
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
 
LVL 1

Author Comment

by:ncw
ID: 36546482
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
 
LVL 1

Author Comment

by:ncw
ID: 36548224
Unfortunately my solution updated all checkboxes in the html.
0
 
LVL 1

Accepted Solution

by:
ncw earned 0 total points
ID: 36548357
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
 
LVL 1

Author Closing Comment

by:ncw
ID: 37859639
My comment was the solution for me.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question