Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-22-2013, 09:24 AM   PM User | #1
bemore
New Coder

 
Join Date: Feb 2013
Posts: 39
Thanks: 14
Thanked 0 Times in 0 Posts
bemore is an unknown quantity at this point
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
bemore is offline   Reply With Quote
Old 02-22-2013, 10:13 AM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 574
Thanks: 15
Thanked 64 Times in 64 Posts
Arcticwarrio is on a distinguished road
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 
  #-----
?>
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.

Last edited by Arcticwarrio; 02-22-2013 at 10:19 AM..
Arcticwarrio is offline   Reply With Quote
Old 02-22-2013, 11:08 AM   PM User | #3
bemore
New Coder

 
Join Date: Feb 2013
Posts: 39
Thanks: 14
Thanked 0 Times in 0 Posts
bemore is an unknown quantity at this point
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!
bemore is offline   Reply With Quote
Old 02-22-2013, 12:05 PM   PM User | #4
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 574
Thanks: 15
Thanked 64 Times in 64 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Users who have thanked Arcticwarrio for this post:
bemore (02-22-2013)
Old 02-22-2013, 01:09 PM   PM User | #5
bemore
New Coder

 
Join Date: Feb 2013
Posts: 39
Thanks: 14
Thanked 0 Times in 0 Posts
bemore is an unknown quantity at this point
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
bemore is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:25 AM.


Advertisement
Log in to turn off these ads.