【问题标题】:Regex for removing spaces but outside the < and > ,Php用于删除空格但在 < 和 > ,Php 之外的正则表达式
【发布时间】:2014-12-01 09:41:34
【问题描述】:

我已经编写了正则表达式,用于从字符串中删除除 中的数据之外的所有空格,但它不起作用,我无法获得解决方案。

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

当我运行正则表达式时,它会删除所有空格

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

我尝试过的是:

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

我想要的结果应该是这样的:

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

注意:href 和 span 类之间的空格,但保留开始和结束标签内的空格

【问题讨论】:

    标签: php regex


    【解决方案1】:
    [ ]+|(<([^ ]+)[^>]+>[^<]+<\/\2>)|(<[^>]+>)
    

    试试这个。替换为$1$3。查看演示。

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

        $re = "/[ ]+|(<([^ ]+)[^>]+>[^<]+<\\/\\2>)|(<[^>]+>)/";
    $str = "<style>\n .ebayFont{font-family: trebuchet ms;}\n .colorRed{color:red;}\n .productDescription{marign-top:20px;font-size: large;}\n .gaurantedLogo{float: right;margin-right: -215px;margin-top: 3px;opacity: 0.99;width: 107px;}\n .comment{float: right;font-size: 15px;margin-left: 320px;margin-top: 5px;position: absolute;width: 487px;}\n .email{color: white;float: right;font-size: 19px;margin-left: 500px;margin-top: 25px;position: absolute;}\n .phone{color: white;float: right;font-size: 21px;margin-left: 290px;margin-top: 25px;position: absolute;}\n .productImage{float:left;width: 35%;height:85%;}\n .productShortDescription{float:right;width: 62%;}\n </style>\n <div>\n <table width=\"800px\">\n <tbody>\n <tr>\n <td colspan=\"2\" align=\"center\">\n <h2 class=\"ebayFont\" style=\"margin-left: 89px;\">Adidas Deodorant - Adidas Ice Dive Deo - For Men - 150 ML</h2>\n <h3 class=\"colorRed ebayFont\" style=\"margin-left: 48px;\">100% Genuine - No Fake Products - Fast Delivery</h3>\n <td>\n </tr>\n <tr>\n <td>\n <div class=\"productImage\">\n <img src=\"/gauranteed%2Blogo.png\" class=\"gaurantedLogo\">\n <img style=\"width:500px;z-index:-1;margin-top:50px;\" src=\"/prf100958.jpg\">\n </div>\n <div class=\"productShortDescription\" style=\"width:300px\">\n <div>\n <h4 class=\"ebayFont\">PRODUCT INFO:</h4>    \n <p class=\"ebayFont\">\n 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\n </p>\n </div>\n <div>\n <h4 class=\"ebayFont\">BRAND INFO:</h4>    \n <p class=\"ebayFont\">\n 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.\n </p>\n </div>\n <div>\n <h4 class=\"ebayFont\">BEST FOR:</h4>    \n <p class=\"ebayFont\">\n Night Out \n </p>\n </div>\n <div>\n <h4 class=\"ebayFont\">GENUINE PRODUCT:</h4>    \n <p class=\"ebayFont\">\n Be assured that we sell only 100% authentic and imported products. Every product featured on our website is sourced from licensed & authorized dealers only. \n </p>\n </div>\n <div>\n <h4 class=\"ebayFont\">RETURN POLICY:</h4>    \n <p class=\"ebayFont\">\n 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*\n </p>\n </div>\n </div>\n </td> \n </tr>\n <tr>\n <td>\n <div style=\"float: right;\">\n <img src=\"logo_1.jpg\">\n </div>\n </td>\n </tr><tr>\n <td>\n <div>\n <div class=\"comment ebayFont\">Very Good Fragnance</div>\n <img src=\"/whatusersaresaying2.png\" style=\"width:800px;position: relative;z-index:-1;\">\n </div>\n </td>\n </tr> \n <tr>\n <td>\n <div>\n <img src=\"/footer+inst.png\" style=\"width:800px;position: relative;\">\n </div>\n </td>\n </tr>\n <tr>\n <td>\n <div>\n <span class=\"phone ebayFont\">180013001400</span>\n <span class=\"email ebayFont\">tpsales@collection.com</span>\n <img src=\"/footer.png\" style=\"width:800px;position: relative;z-index:-1;\">\n </div>\n </td>\n </tr>\n </tbody>\n </table>\n </div>";
    $subst = "$1$3";
    
    $result = preg_replace($re, $subst, $str);
    

    【讨论】:

    【解决方案2】:

    使用下面的正则表达式匹配标签外的所有空格。

    \h+(?![^<>]*>)
    

    DEMO

    通过用空字符串替换所有匹配的空格将为您提供所需的输出。 \h 将匹配所有水平空格。

    $file = "I am a boy, <a href=abcd.com\">click</a> to <span class='image'>add me</span>";
    echo preg_replace('~\h+(?![^<>]*>)~', '', $file);
    

    输出:

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

    【讨论】:

    • 如果我们必须在 和 > 之间保留空格,那么要更改什么。比如 点击我
    • 你能提供反引号的例子吗?
    • I ama boy,&lt;a href=abcd.com"&gt;click&lt;/a&gt; to &lt;span class='image'&gt;add me&lt;/span&gt; 现在结果应该是这样的:Iamaboy,&lt;a href=abcd.com"&gt;click&lt;/a&gt;to&lt;span class='image'&gt;add me&lt;/span&gt; 保持加我空间
    • 如果输入是&lt;h1&gt;foo bar&lt;a href="foo.com"&gt;foo bar&lt;/a&gt; foo bar&lt;/h1&gt;,你的输出是什么?
    • &lt;h1&gt;foobar&lt;a href="foo.com"&gt;foobar&lt;/a&gt;foobar&lt;/h1&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2012-02-24
    • 2023-04-08
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多