dpp3047 2014-12-01 09:41
浏览 31
已采纳

正则表达式删除空格但在<和>之外,Php

I have written the regex for removing all spaces from my string except the data inside the < >,But its not working and I am unable to get the solution.

Example: I have a string like "I am a boy, <a href=abcd.com">click</a> to <span class='image'>add me</span>  

When I run the regex it removes all the spaces as

"Iamaboy,<ahref=abcd.com">click</a>to<spanclass='image'>addme</span>" 

What I have tried is as:

preg_replace('/\s+(?=\\<)|(?<=\\>)\s+/', '', $data);

Result that I want is supposed to be like:

"Iamaboy,<a href=abcd.com">click</a>to<span class='image'>add me</span>"

NOTE: The spaces between a href and span class but maintain the spaces inside the opening and closing tags

  • 写回答

2条回答 默认 最新

  • dsa4214 2014-12-01 09:43
    关注
    [ ]+|(<([^ ]+)[^>]+>[^<]+<\/\2>)|(<[^>]+>)
    

    Try this.Replace by $1$3.See demo.

    http://regex101.com/r/vF0kU2/4

        $re = "/[ ]+|(<([^ ]+)[^>]+>[^<]+<\\/\\2>)|(<[^>]+>)/";
    $str = "<style>
     .ebayFont{font-family: trebuchet ms;}
     .colorRed{color:red;}
     .productDescription{marign-top:20px;font-size: large;}
     .gaurantedLogo{float: right;margin-right: -215px;margin-top: 3px;opacity: 0.99;width: 107px;}
     .comment{float: right;font-size: 15px;margin-left: 320px;margin-top: 5px;position: absolute;width: 487px;}
     .email{color: white;float: right;font-size: 19px;margin-left: 500px;margin-top: 25px;position: absolute;}
     .phone{color: white;float: right;font-size: 21px;margin-left: 290px;margin-top: 25px;position: absolute;}
     .productImage{float:left;width: 35%;height:85%;}
     .productShortDescription{float:right;width: 62%;}
     </style>
     <div>
     <table width=\"800px\">
     <tbody>
     <tr>
     <td colspan=\"2\" align=\"center\">
     <h2 class=\"ebayFont\" style=\"margin-left: 89px;\">Adidas Deodorant - Adidas Ice Dive Deo - For Men - 150 ML</h2>
     <h3 class=\"colorRed ebayFont\" style=\"margin-left: 48px;\">100% Genuine - No Fake Products - Fast Delivery</h3>
     <td>
     </tr>
     <tr>
     <td>
     <div class=\"productImage\">
     <img src=\"/gauranteed%2Blogo.png\" class=\"gaurantedLogo\">
     <img style=\"width:500px;z-index:-1;margin-top:50px;\" src=\"/prf100958.jpg\">
     </div>
     <div class=\"productShortDescription\" style=\"width:300px\">
     <div>
     <h4 class=\"ebayFont\">PRODUCT INFO:</h4>    
     <p class=\"ebayFont\">
     Adidas Ice Dive cologne is sporty fresh, with vibrant citrus scents accented by soft masculine woods. Top notes of grapefruit, lavender and mint lead into a black pepper, bamboo, and kiwi heart and a base composed of sandalwood, tonka bean, grey amber and vanilla. Notes:Top Note: Kiwi, Lavender, Madarin Orange, Yuzu, Mint, Grapefruit, Anise, Bergamot Middle Note: Sandalwood, Patchouli, Geranium Base Note: Tonka Bean, Muck, Vanilla. Pepper, Ambergris
     </p>
     </div>
     <div>
     <h4 class=\"ebayFont\">BRAND INFO:</h4>    
     <p class=\"ebayFont\">
     A brainchild of Adolf Dassler, Adidas is one of the leading sportswear manufacturers in the world. Each and every product manufactured under the brand’s umbrella speaks for immense comfort. Apart from proffering sports clothing,shoes and accessories, this international brand also manufactures eye wear, bags, watches, and sports related goods.
     </p>
     </div>
     <div>
     <h4 class=\"ebayFont\">BEST FOR:</h4>    
     <p class=\"ebayFont\">
     Night Out 
     </p>
     </div>
     <div>
     <h4 class=\"ebayFont\">GENUINE PRODUCT:</h4>    
     <p class=\"ebayFont\">
     Be assured that we sell only 100% authentic and imported products. Every product featured on our website is sourced from licensed & authorized dealers only. 
     </p>
     </div>
     <div>
     <h4 class=\"ebayFont\">RETURN POLICY:</h4>    
     <p class=\"ebayFont\">
     Although we only sell original products and never compromise on quality, we offer 100% money back guarantee if a product is found to be counterfeit. Terms and Conditions apply*
     </p>
     </div>
     </div>
     </td> 
     </tr>
     <tr>
     <td>
     <div style=\"float: right;\">
     <img src=\"logo_1.jpg\">
     </div>
     </td>
     </tr><tr>
     <td>
     <div>
     <div class=\"comment ebayFont\">Very Good Fragnance</div>
     <img src=\"/whatusersaresaying2.png\" style=\"width:800px;position: relative;z-index:-1;\">
     </div>
     </td>
     </tr> 
     <tr>
     <td>
     <div>
     <img src=\"/footer+inst.png\" style=\"width:800px;position: relative;\">
     </div>
     </td>
     </tr>
     <tr>
     <td>
     <div>
     <span class=\"phone ebayFont\">180013001400</span>
     <span class=\"email ebayFont\">tpsales@collection.com</span>
     <img src=\"/footer.png\" style=\"width:800px;position: relative;z-index:-1;\">
     </div>
     </td>
     </tr>
     </tbody>
     </table>
     </div>";
    $subst = "$1$3";
    
    $result = preg_replace($re, $subst, $str);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!