PHP/REGEX: Set width of first th

Using PHP and preg_replace, I want to set the width of the first column in my table to 20%.

This:
<table class="mytable">
<thead>
<tr>
<th scope="col">
First Column</th>
<th scope="col" class="textright">
Second COlumn</th>
<th scope="col">
Third Column</th>
</tr>
</thead>
</table>

Open in new window

Should become this:

<table class="mytable">
<thead>
<tr>
<th scope="col" width="20%">
First Column</th>
<th scope="col" class="textright">
Second COlumn</th>
<th scope="col">
Third Column</th>
</tr>
</thead>
</table>

Open in new window

LVL 16
hankknightAsked:
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.

gr8gonzoConsultantCommented:
The preg_replace does have an option to limit the number of replacements it does, so you could try just doing something like:

$your_html = preg_replace("/scope=\"col\">/","scope=\"col\" width=\"20%\",$your_html,1);

If you need something more precise, then I would use something like PHP Simple HTML DOM Parser:
http://simplehtmldom.sourceforge.net/

...to find and update the tag using CSS-like matches.

0
gr8gonzoConsultantCommented:
By the way, if you use the Simple HTML DOM Parser, your code would look something like:

$html = str_get_html('<html><body>...<table class="mytable">...etc...</table>...</body></html>');

// Find all tables with the CSS class of "mytable"
foreach($html->find('table.mytable) as $mytable) {

     // For each found table, find the first <th> that has the scope="col" attribute
     $th     = $mytable->find('th[scope=col]', 0);
     $th->width = "20%"; // Set the <th> "width" attribute to "20%"
 }

then output the modified structure:
echo $html->save();
0
hankknightAuthor Commented:
Why doesn't this work?
<?php

header('Content-Type:text/plain'); 

$str = '
<table>
<thead>
<tr>
<th>
Col 1</th>
<th>
Col 2</th>
<th>
Col 3</th>
</tr>
</thead>
</table>
';

$str = preg_replace('/<th/', '<th width="100%" ', $str, 1); 

echo $str;

?>

Open in new window

0
käµfm³d 👽Commented:
Why doesn't this work?
Because you have a <thead> tag, which begins with "<th". Try adding the closing angle bracket to your pattern.

$str = preg_replace('/<th>/', '<th width="100%" ', $str, 1);

Open in new window

0
käµfm³d 👽Commented:
...and if there is a chance that your <th> tag has attributes, then you could use:

$str = preg_replace('/<th( |>)/', '<th width="100%"$1', $str, 1);

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
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.

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.