0

i got this table generated with php: a function generates a string with all the html code:

<table><tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td></tr><tr><td>2</td><td>4</td><td>6</td><td>8</td><td>10</td><td>12</td><td>14</td><td>16</td><td>18</td><td>20</td></tr><tr><td>3</td><td>6</td><td>9</td><td>12</td><td>15</td><td>18</td><td>21</td><td>24</td><td>27</td><td>30</td></tr><tr><td>4</td><td>8</td><td>12</td><td>16</td> .... </table>

now i want to make the numbers 1 to 10 black. i'm trying to replace '<td>(10|[0-9])</td>' with <td style="font-weight: bold">THE-ORIGINAL-NUMBER</td>.

Thanx in advance!

p.s. i know there're alot of similir answers out there but i just couldnt figure it out.. is there an actually noob-friendly tut/glossary of regex out there? i couldn't really find a modern day site.

sommmen
  • 6,570
  • 2
  • 30
  • 51
  • if you can guarantee that the html is going to look like how you say but you should really read http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – wirey00 Oct 16 '14 at 20:58
  • It would be better CSS/HTML-wise to give all the relevant TD elements a class (e.g. ``) and then define the css class as being `font-weight: bold`. That way, if you decide to change the appearance of these table elements, or even decide you no longer want those figures highlighted, you can just change the css declaration; you don't have to edit the page source. – i alarmed alien Oct 16 '14 at 21:02
  • @ᾠῗᵲᄐᶌ that thread hurt my brain. – sommmen Oct 16 '14 at 21:04
  • @i_alarmed_alien i know, this was just an example for school. thnxs for pointing that out though! – sommmen Oct 16 '14 at 21:05

3 Answers3

1

If you are matching this regular expression:

<td>(10|[0-9])</td>

You are capturing 10|[0-9] into capture group #1. This can be referenced in your replacement with either of the following backreferences:

\1
$1

Full PHP code:

$html = '<td>1</td>';
$html = preg_replace(
  '~<td>(10|[0-9])</td>~',
  '<td style="font-weight: bold">\1</td>',
  $html
);
Sam
  • 20,096
  • 2
  • 45
  • 71
1

use this regex

(?<=<td>)(10|[0-9])(?=<\/td>)

replace group #1 with:

<span class="BoldText">$1</span>

Style:

.BoldText {
    font-weight: bold;
}
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
0

using <b> may be useful:

replace

'~<td>(10|[0-9])</td>~'

with

'<td><b>\1</b></td>'
Farvardin
  • 5,336
  • 5
  • 33
  • 54