Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Php preg_replace to set checkbox to checked and readonly

Posted on 2011-09-15
15
Medium Priority
?
531 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
  • 8
  • 7
15 Comments
 
LVL 111

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 111

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

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 111

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 111

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 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

783 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