replace string in php

Hi, I have a table:

 <tr> <td>  <input type="checkbox" name=
                       "irena1" class="chkrows" unchecked> </td>  </tr>

<tr> <td>  <input type="checkbox" name=
                       "irena2" class="chkrows" unchecked> </td>  </tr>

etc.

I have my names in an array e.g. names["irena1"]

now I want to switch "unchecked" to "check" for all those rows where the name is in names
array. What is the fastest way ?
RozamundaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
themrrobertConnect With a Mentor Commented:
Store your table into $table


$tabs = split('name=',$table);  //split, be sure to use single quotes

foreach($names as $name) 
  foreach($tabs as $k => $value)
    if(strpos($value,$name)!==false)  //match found, be sure to use !==
      $tabs[$k] = str_replace("unchecked","checked",$value);
$table = join('name=',$tabs);

Open in new window

0
 
Michael701Commented:
Do you mean an array like  
$names["irena1"], $names["irena2"]
or
$names[0]="irena1", $names[1]="irena2"

if not sure, do this and post the output

print_r ($name);
---------
It would be easiest to check for the name in the array as you are generating the html code. Are you building the html code in php? if so, post that code.
0
 
RozamundaAuthor Commented:
The latter one
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Hugh McCurdyCommented:
Following up on Michael.  I think it would really help if you posted your code.  Tell us what you think it's doing now and what you'd like it to do instead.
0
 
Cornelia YoderArtistCommented:
<tr> <td>  <input type="checkbox" name=
                       "irena2" class="chkrows" <? if (in_array($namesarray,"irena2") echo "unchecked" else echo "checked"; ?> > </td>  </tr>
0
 
Cornelia YoderArtistCommented:
ooops, I think I got the checked/unchecked backwards from what you wanted, not to mention a syntax error :)

<tr> <td>  <input type="checkbox" name=
                       "irena2" class="chkrows"
<? if (in_array($namesarray,"irena2"))
      echo "checked"
    else
      echo "unchecked";
?>
 > </td>  </tr>
0
 
Michael701Commented:
yodercm: that's where I was going. I'm getting better at knowing that what they say isn't what they mean, when posting questions.

If they are building the html from a database (or another array of $all_names[]) then we could help with the code.
0
 
Ray PaseurCommented:
@Michale701: what they say isn't what they mean, when posting questions

In a word.  Listening through the question all the way to the central issue is something of an art form and a task of discovery!
0
 
DerokorianCommented:
Also I would do this:

<tr> <td>  <input type="checkbox" name="irena2" class="chkrows" 
<? echo !in_array($namesarray,"irena2") ? 'un' : ''; ?>checked > </td>  </tr>

Open in new window

0
 
DerokorianCommented:
Except that I would use short_tags...

<tr> <td>  <input type="checkbox" name="irena2" class="chkrows" 
<?php echo !in_array($namesarray,"irena2") ? 'un' : ''; ?>checked > </td>  </tr>

Open in new window

0
 
DerokorianCommented:
**wouldn't

Damn i just can't type anything right today.... (in fact i typed write instead of right at first....)
0
 
RozamundaAuthor Commented:
No I am not building the html, I already have the html (it was built somewhere elsewhere).
If I was asking how to build a table then I would have said so, that's why I started my question with
"I have a table'. So I will go with   themrrobert: solution.
0
All Courses

From novice to tech pro — start learning today.