Heim  >  Artikel  >  Backend-Entwicklung  >  <tr><td>测试</td><tr>想替换为<LI>测试</LI>求PHP 正则替换,多谢

<tr><td>测试</td><tr>想替换为<LI>测试</LI>求PHP 正则替换,多谢

WBOY
WBOYOriginal
2016-06-13 11:51:231061Durchsuche
测试 想替换为
  • 测试
  • 求PHP 正则替换,谢谢。
    如题:
    测试 想替换为
  • 测试
  • 求PHP 正则替换,谢谢。

    因为我的HTML代码如下:
    <br /><table width="96%"border="0" align="center" cellpadding="4" cellspacing="0"><br /><br />        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';"><br />          <td><a href="news_detail.php?infoId=141" class="link">新闻一</a></td><br />          <td align="right" class="newstime">2014-03-12</td><br />        </tr><br /><br />        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';"><br />          <td><a href="news_detail.php?infoId=140" class="link">新闻二</a></td><br />          <td align="right" class="newstime">2014-02-20</td><br />        </tr><br /></table><br /><br /><br />


    想替换成
    <br /><ul data-role="listview"  ><br />    <li><a href="#">新闻一 2014-03-12</a></li><br />    <li><a href="#">新闻二 2014-02-20</a></li><br /> <br /></ul><br />



    ------解决方案--------------------
    先匹配。再拼装结果。
    $s=<<< TXT<br /><br /><table width="96%"border="0" align="center" cellpadding="4" cellspacing="0"><br /><br />        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';"><br />          <td><a href="news_detail.php?infoId=141" class="link">新闻一</a></td><br />          <td align="right" class="newstime">2014-03-12</td><br />        </tr><br /><br />        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';"><br />          <td><a href="news_detail.php?infoId=140" class="link">新闻二</a></td><br />          <td align="right" class="newstime">2014-02-20</td><br />        </tr><br /></table><br />TXT;<br /> preg_match_all('#<tr[^>]+>\s*<td><a[^>]+>(.+?)</a></td>\s*<td[^>]+>(.+?)<#s',$s,$m);<br /><br />$html="<ul data-role=\"listview\"  >\n";<br />foreach($m[1] as $k=>$v) $html .= "<li><a href=\"#\">$v {$m[2][$k]}</a></li>\n";<br />$html .= '</ul>';<br />echo $html;

    ------解决方案--------------------
    用str_replace要好一点吧 
    Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn