测试想替换为测试求PHP 正则替换,谢谢。

php中文网
发布: 2016-06-23 14:00:44
原创
1360人浏览过

如题:

测试 想替换为
  • 测试
  • 求PHP 正则替换,谢谢。

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


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


    白瓜面试
    白瓜面试

    白瓜面试 - AI面试助手,辅助笔试面试神器

    白瓜面试 40
    查看详情 白瓜面试

    回复讨论(解决方案)

    先匹配。再拼装结果。

    $s=<<< TXT<table width="96%"border="0" align="center" cellpadding="4" cellspacing="0">        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';">          <td><a href="news_detail.php?infoId=141" class="link">新闻一</a></td>          <td align="right" class="newstime">2014-03-12</td>        </tr>        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';">          <td><a href="news_detail.php?infoId=140" class="link">新闻二</a></td>          <td align="right" class="newstime">2014-02-20</td>        </tr></table>TXT; preg_match_all('#]+>\s*]+>(.+?)\s*]+>(.+?)<#s',$s,$m);$html="';echo $html;
    登录后复制

    用str_replace要好一点吧 

    先匹配。再拼装结果。

    $s=<<< TXT<table width="96%"border="0" align="center" cellpadding="4" cellspacing="0">        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';">          <td><a href="news_detail.php?infoId=141" class="link">新闻一</a></td>          <td align="right" class="newstime">2014-03-12</td>        </tr>        <tr   onMouseOver="this.style.backgroundColor='#EAEAEA'; return true;" onMouseOut="this.style.backgroundColor='transparent';">          <td><a href="news_detail.php?infoId=140" class="link">新闻二</a></td>          <td align="right" class="newstime">2014-02-20</td>        </tr></table>TXT; preg_match_all('#]+>\s*]+>(.+?)\s*]+>(.+?)<#s',$s,$m);$html="';echo $html;
    登录后复制


    非常感谢版主的回答
    测试了一下,果然是有效果的。

    不过我还想 超链接那一段能够有具体地址,形如:
    <ul data-role="listview"  >    <li><a href="news_detail.php?infoId=141">新闻一 2014-03-12</a></li>    <li><a href="news_detail.php?infoId=140">新闻二 2014-02-20</a></li>  </ul
    登录后复制


    版主高手啊,正则 还有 FOREACH能不能大概给个解释,谢谢啊。

    高手,分不够还可以再加~~~~
    谢谢。

     preg_match_all('#<tr[^>]+>\s*<td><a\s*href="(.+?)"[^>]+>(.+?)</a></td>\s*<td[^>]+>(.+?)<#s',$s,$m);$html="<ul data-role=\"listview\"  >\n";foreach($m[1] as $k=>$v) $html .= "<li><a href=\"$v\">{$m[2][$k]} {$m[3][$k]}</a></li>\n";$html .= '</ul>';echo $html;
    登录后复制

    版主非常强大啊~~~
    赞一个~
    结贴,哈。

    PHP速学教程(入门到精通)
    PHP速学教程(入门到精通)

    PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号