用于删除空格但正在<之外的正则表达式和> ,PHP

时间:2014-12-01 09:41:35

标签: php regex

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

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类之间的空格但保留开始和结束标记内的空格

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

使用以下正则表达式匹配标记之外的所有空格。

\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>