Shivdhwaj Pandey
Shivdhwaj Pandey

Reputation: 157

Regex for removing spaces but outside the < and > ,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

Upvotes: 0

Views: 77

Answers (2)

vks
vks

Reputation: 67968

[ ]+|(<([^ ]+)[^>]+>[^<]+<\/\2>)|(<[^>]+>)

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

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\">[email protected]</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);

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

Use the below regex to match all the spaces which are present outside the tags.

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

DEMO

BY replacing all the matched spaces with empty string will give you the desired output. \h would match all the horizontal spaces.

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

Output:

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

Upvotes: 0

Related Questions