1.要求:

对于:

1 $str = '<p></p><p style="text-indent:241px"><strong><span style=";font-family:宋体;font-weight:bold;font-size:16px">A</span></strong></p>
<table width="492"><tbody><tr class="firstRow" style="height:62px"><td style="padding: 0px 7px; border-width: 1px; border-style: solid; border-color: windowtext;" valign="top" width="450">
<p><span style=";font-family:宋体;font-size:14px">A. Give them time to relax</span><span style="font-family:\'Times New Roman\';font-size:14px">.</span>
<span style=";font-family:宋体;font-size:14px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p><span style=";font-family:宋体;font-size:14px">
B. Lily and I should go say hello</span><span style="font-family:\'Times New Roman\';font-size:14px">.</span></p><p><span style=";font-family:宋体;font-size:14px">
C. That</span><span style="font-family:\'Times New Roman\';font-size:14px">’</span><span style=";font-family:宋体;font-size:14px">
s a good idea. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p><span style=";font-family:宋体;font-size:14px">
D. Do you know the kid</span><span style="font-family:\'Times New Roman\';font-size:14px">’</span><span style=";font-family:宋体;font-size:14px">s names?</span>
</p><p><span style=";font-family:宋体;font-size:14px">E. Could we take them some drinks?</span></p></td></tr></tbody></table>';

其中对于的text-index=241px, width="492" , width="450" 要转为以560为基准的百分比的形式. 例如 width="75%";

2.代码:

 1 $str = '<p></p><p style="text-indent:241px"><strong><span style=";font-family:宋体;font-weight:bold;font-size:16px">A</span>
</strong></p><table width="492"><tbody><tr class="firstRow" style="height:62px">
<td style="padding: 0px 7px; border-width: 1px; border-style: solid; border-color: windowtext;" valign="top" width="450">
<p><span style=";font-family:宋体;font-size:14px">A. Give them time to relax</span><span style="font-family:\'Times New Roman\';font-size:14px">.
</span><span style=";font-family:宋体;font-size:14px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
<p><span style=";font-family:宋体;font-size:14px">B. Lily and I should go say hello</span>
<span style="font-family:\'Times New Roman\';font-size:14px">.</span></p><p><span style=";font-family:宋体;font-size:14px">
C. That</span><span style="font-family:\'Times New Roman\';font-size:14px">’</span><span style=";font-family:宋体;font-size:14px">
s a good idea. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p><span style=";font-family:宋体;font-size:14px">
D. Do you know the kid</span><span style="font-family:\'Times New Roman\';font-size:14px">’</span><span style=";font-family:宋体;font-size:14px">s names?</span>
</p><p><span style=";font-family:宋体;font-size:14px">E. Could we take them some drinks?</span></p></td></tr></tbody></table>'; 2 echo $str; 3 4 5 6 7 $preg = '/width="\s*(\d+)(px)?\s*"/'; 8 // preg_match_all($preg, $str , $res); 9 // var_dump($res); 10 echo "\n"; 11 $first = preg_replace_callback($preg, 'chuli', $str); 12 13 echo 'first:' .$first; 14 15 function chuli($matches) 16 { 17 $per = floor($matches[1]/560 * 100) . '%'; 18 // echo "$per" .'--'; 19 return 'width="' .$per. '"'; 20 } 21 22 $preg2 = '/text-indent:\s*(\d+)(px)?/'; 23 $second = preg_replace_callback($preg2, 'chuli2', $first); 24 echo 'second' .$second; 25 26 function chuli2($matches) 27 { 28 $per = floor($matches[1]/560 * 100) . '%'; 29 return 'text-indent:' . $per; 30 }

对于preg_replace_callback($preg2, 'chuli', $first) :

1. 调用的回调函数, 写成 chuli  , 不是chuli()

2. 定义的函数chuli($matches)这个是$matches是固定的.  其中$matches[0] 是匹配到的字符串. width="490", $matches[1]是分组1,就是 (\d+) ,就是 490 .

    最后返回的是  width="23%" ,用于代替匹配到的 width="130".

3.这个会多次匹配, 第一次匹配到widh="492" ,调用chuli进行处理, 第二次匹配到width="450", 继续调用chuli进行处理

-------------------

3.结果:

 

 

 

--------------

 

 

 

 

 

 

------------