Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts

    RegEx to change format of input

    I'm using

    Code:
    $cleangc = preg_replace("/[^a-zA-Z0-9\s]/", "", $gamecode);
    $cleangc = strtoupper  ($cleangc);
    Which does do most of the job, but I'm wondering if this can also make the output character format to be something like [^a-zA-Z\s]-[0-9]

    For example slus_123.45 would output as SLUS-12345

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    PHP Code:
     <?php
      
    # URL that generated this code:
      # http://txt2re.com/index-php.php3?s=slus_123.45&2&-22&3&-16&4

      
    $txt='slus_123.45';

      
    $re1='((?:[a-z][a-z]+))';    # Word 1
      
    $re2='(_)';    # Any Single Character 1
      
    $re3='(\\d+)';    # Integer Number 1
      
    $re4='(\\.)';    # Any Single Character 2
      
    $re5='(\\d+)';    # Integer Number 2

      
    if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is"$txt$matches))
      {
          
    $word1=strtoupper($matches[1][0]);
          
    $c1=$matches[2][0];
          
    $int1=$matches[3][0];
          
    $c2=$matches[4][0];
          
    $int2=$matches[5][0];
          print 
    "$word1-$int1$int2 \n";
      }

      
    #-----
      # Paste the code into a new php file. Then in Unix:
      # $ php x.php 
      #-----
    ?>
    Last edited by Arcticwarrio; 02-22-2013 at 11:19 AM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts
    That works well if slus_123.45 was the only acceptable format.. but if the user enters any other format nothing is inserted.

    I need the format of anything entered to change, basically..

    So if slus-12345 is entered, it stays the same. If slus12345 is entered, it's changed to slus-12345... if slus_123.45 is entered, it changes to slus-12345..

    I feel like this can be accomplished somehow with the simple code that I am already using.

    Here is my current method to just replace everything with ""
    Code:
    if(isset($_POST['gamecode'])) {
    $gamecode = ($_POST['gamecode']); 
    $cleangc = preg_replace("/[^a-zA-Z0-9\s]/", "", $gamecode);
    $cleangc = strtoupper  ($cleangc);   
    } else {
    $gamecode = "None";
    }
    I'm looking for something that instead of replacing everything with "", instead it'd be like
    Code:
    $cleangc = preg_replace("/([^a-zA-Z]) ([0-9\s])/", "$1-$2", $gamecode);
    Accept working xD
    Any tip appreciated!

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    This works for all the variants you gave.

    and also "coding forums_245.54.3"

    PHP Code:
    <?php

    if(isset($_POST['gamecode'])) {
      
    $txt=$_POST['gamecode'];

      
    $re1='((?:[a-z][a-z]+))';    # Word 1
      
    $re2='.*?';    # Non-greedy match on filler
      
    $re3='((?:[a-z][a-z]+))';    # Word 2
      
    $re4='.*?';    # Non-greedy match on filler
      
    $re5='(\\d+)';    # Integer Number 1
      
    $re6='.*?';    # Non-greedy match on filler
      
    $re7='(\\d+)';    # Integer Number 2
      
    $re8='.*?';    # Non-greedy match on filler
      
    $re9='(\\d+)';    # Integer Number 3
      
    $re10='.*?';    # Non-greedy match on filler
      
    $re11='(\\d+)';    # Integer Number 4
      
    $re12='.*?';    # Non-greedy match on filler
      
    $re13='(\\d+)';    # Integer Number 5

      
    if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10.$re11.$re12.$re13."/is"$txt$matches))
      {
          
    $word1=strtoupper($matches[1][0]);
          
    $word2=strtoupper($matches[2][0]);
          
    $int1=$matches[3][0];
          
    $int2=$matches[4][0];
          
    $int3=$matches[5][0];
          
    $int4=$matches[6][0];
          
    $int5=$matches[7][0];
          
    $cleangc "$word1$word2-$int1$int2$int3$int4$int5 \n";
      }
      
    } else {
    $cleangc "None";
    }
    echo 
    $cleangc;
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    bemore (02-22-2013)

  • #5
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    This works for all the variants you gave.

    and also "coding forums_245.54.3"
    That is working perfectly... thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •