• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

请教一个正则问题

php 搞代码 3年前 (2022-01-23) 13次浏览 已收录 0个评论
文章目录[隐藏]

原字符串:



  • la belle
    courbe de


    Parfait comme
    cadeau pour votre amoureux


    NOTE
    : La9来源gaodai#ma#com搞@代~码$网搞gaodaima代码ver
    à la main

  • 处理后的字符串:


  • la belle courbe de

    Parfait comme cadeau pour votre amoureux

    NOTE : Laver à la main

  • 问题:
    请问如何写正则
    preg_replace();

    回复讨论(解决方案)

    $arr1=array(‘#]+>([^<]+)\r\n#i’, ‘#
    ]*>#i’, ‘#\r\n#i’);
    $arr2=array(‘$1 ‘, ‘
    ‘, ”);
    preg_replace($arr1, $arr2, $str);

    \r\n是windows文本换行符,要注意对应文本的格式
    没环境测试,自己摸索一下吧

    $str1 = ‘



  • la belle
    courbe de


    Parfait comme
    cadeau pour votre amoureux


    NOTE
    : Laver
    à la main

  • ‘;

    $reg = “/|<\/span>|zd=\”(.*)\”|closure_uid_235198886=\”(.*)\”/iU”;
    $str = preg_replace($reg, ”, $str1);

    var_dump($str);exit;

    $s =<<< TXT<LI>  <SPAN id="result_box" lang="fr">    <SPAN class="hps">la belle</SPAN>     <SPAN class="hps">courbe de</SPAN>     <BR>    <SPAN class="hps">Parfait comme</SPAN>     <SPAN class="hps">cadeau pour votre amoureux</SPAN>    <BR>    <SPAN class="hps">NOTE</SPAN>    <SPAN>: Laver</SPAN>     <SPAN class="hps">à la main</SPAN>   </SPAN> <LI>TXT;echo strip_tags($s, '<li><br>');

    $str1 = '<LI>      <SPAN id="result_box" lang="fr">        <SPAN class="hps">la belle</SPAN>        <SPAN class="hps">courbe de</SPAN>        <BR>        <SPAN class="hps">Parfait comme</SPAN>        <SPAN class="hps">cadeau pour votre amoureux</SPAN>        <BR>        <SPAN class="hps">NOTE</SPAN>        <SPAN>: Laver</SPAN>        <SPAN class="hps">à la main</SPAN>      </SPAN>    <LI>';$reg = "/<span>|<\/span>|zd=\"(.*)\"|closure_uid_235198886=\"(.*)\"/iU";$str = preg_replace($reg, '', $str1);var_dump($str);exit;

    $str1 = '<LI>      <SPAN id="result_box" lang="fr">        <SPAN class="hps">la belle</SPAN>        <SPAN class="hps">courbe de</SPAN>        <BR>        <SPAN class="hps">Parfait comme</SPAN>        <SPAN class="hps">cadeau pour votre amoureux</SPAN>        <BR>        <SPAN class="hps">NOTE</SPAN>        <SPAN>: Laver</SPAN>        <SPAN class="hps">à la main</SPAN>      </SPAN>    <LI>';$reg = "/<span>|<\/span>|zd=\"(.*)\"|closure_uid_235198886=\"(.*)\"/iU";$str = preg_replace($reg, '', $str1);var_dump($str);exit;

    这个没有把
    里面的冗余代码去掉。

    $s =<<< TXT<LI>  <SPAN id="result_box" lang="fr">    <SPAN class="hps">la belle</SPAN>     <SPAN class="hps">courbe de</SPAN>     <BR>    <SPAN class="hps">Parfait comme</SPAN>     <SPAN class="hps">cadeau pour votre amoureux</SPAN>    <BR>    <SPAN class="hps">NOTE</SPAN>    <SPAN>: Laver</SPAN>     <SPAN class="hps">à la main</SPAN>   </SPAN> <LI>TXT;echo strip_tags($s, '<li><br>');

    因为我的$s前后都还有HTML代码,这里只是截取了需要替换的部分字符串,所以不能用这种方法,只能用正则。

    $patterns = array(
    ‘/]*>/i’,
    ‘/<\/span>/i’,
    ‘/
    ]*>/i’,
    );
    $replace = array(
    ”,
    ”,

    ‘,
    );
    echo preg_replace($patterns, $replace, $str);

    这个不错。


    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:请教一个正则问题

    喜欢 (0)
    [搞代码]
    分享 (0)
  • 发表我的评论
    取消评论

    表情 贴图 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址