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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

themrrobertCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.