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

WBOY
Release: 2016-06-13 11:51:23
Original
1082 people have browsed it
测试 想替换为
  • 测试
  • 求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 />
    Copy after login


    想替换成
    <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 />
    Copy after login



    ------解决方案--------------------
    先匹配。再拼装结果。
    $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;
    Copy after login

    ------解决方案--------------------
    用str_replace要好一点吧 
    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!