Jump to content

Recommended Posts

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);

 

Link to comment
https://forums.phpfreaks.com/topic/313496-formatting-string-help/
Share on other sites

<?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
    }  
      
?>

 

 

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

?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.