CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   RegEx to change format of input (http://www.codingforums.com/showthread.php?t=288110)

bemore 02-22-2013 09:24 AM

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

Arcticwarrio 02-22-2013 10:13 AM

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


bemore 02-22-2013 11:08 AM

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!

Arcticwarrio 02-22-2013 12:05 PM

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


bemore 02-22-2013 01:09 PM

Quote:

Originally Posted by Arcticwarrio (Post 1315243)
This works for all the variants you gave.

and also "coding forums_245.54.3"

That is working perfectly... thank you :thumbsup:


All times are GMT +1. The time now is 07:25 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.