MoFish Posted August 5, 2021 Share Posted August 5, 2021 Hi, I have a textarea called registerDomain which i enter multiple domain names into (separated by a new line) e.g. www.google.com www.sky.com www.bt.com I have the following code to get each of the domains and format them like google.com (lowercase without any spaces) sky.com (lowercase without any spaces) bt.com (lowercase without any spaces) The below code I have works perfectly, however its awful to look at. I wondered if anyone could advise on a cleaner/more elegant way of achieving this. Thanks, if (isset($_POST['submit'])) { $values = trim($_POST['registerDomain']); $array = explode("\n", $values); $array = array_filter($array, 'trim'); foreach ($array as $line) { $domain = addslashes($line); $domain = preg_replace("/\r|\n/", "", $domain); $domain = strtolower($domain); $domain = str_replace("www.", "", $domain); $domain = str_replace(" ", "", $domain); Quote Link to comment https://forums.phpfreaks.com/topic/313496-formatting-string-help/ Share on other sites More sharing options...
FL-RKS Posted August 6, 2021 Share Posted August 6, 2021 <?php // checks if the request exists and isnt empty if(isset($_POST['domains']) && !empty($_POST['domains'])){ // using explode you can also put how mainy domains you want, like, explode("\n",$_POST['domains'], 3), you can change 3 for other numer but // put the number of domains is optional $domains = explode("\n",$_POST['domains']); $validDomains = array(); // check id there are valid domain foreach($domains as $domain){ if(preg_match(/*some regerex expresion to check if element is a valid domain*/, $domain)){ $validDomain = strtolower($domain); $validDomain = str_replace("www." , "" , $validDomain); $validDomain = str_replace(" " , "" , $validDomain); array_push($validDomains, $validDomain) } else { // handle error, like a error message // or continue with the loop without put the domain in $validDomains continue } } // do something with $domains, that is a array // redirect to other page or something } ?> Quote Link to comment https://forums.phpfreaks.com/topic/313496-formatting-string-help/#findComment-1588884 Share on other sites More sharing options...
FL-RKS Posted August 6, 2021 Share Posted August 6, 2021 idk if you can store arrays in a database, but if you cant use this <?php if(isset($_POST['domains']) && !empty($_POST['domains'])){ $domains = explode("\n",$_POST['domains']); $validDomains = array(); foreach($domains as $domain){ if(preg_match(/*regrex expresion*/, $domain)){ $validDomain = strtolower($domain); $validDomain = str_replace("www." , "" , $validDomain); $validDomain = str_replace(" " , "" , $validDomain); array_push($validDomains, $validDomain); } else { continue; } } // new code $domainString = implode(',', $validDomains); //store it in a databse } ?> if you want to convert the string to a array, use this: <?php // example $domainsString = obtainDomainsFromDatabase() $domainsArray = explode(',', $domainsString) // use $domainsArray ?> Quote Link to comment https://forums.phpfreaks.com/topic/313496-formatting-string-help/#findComment-1588885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.