Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression help 2

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
I have a registration code that will always look like this.

1000000001-YZKCGXZS-000000000000

what I need to do is store everything on the left of the first dash in varA, everything between the dashes in varB, and everything after the last dash in varC.

The portion at the beginning will always be 10 characters.
the portion at the end will always be 12 characters.
But the stuff between the dashes can vary in length.

I'm sure I can use regular expressions to do this, but my knowledge of regular expressions is very limited. Could someone provide me with some sample code and a brief explanation of how it all works?

Thanks so much,
Jewel

When faced with a decision, always ask, 'Which would be the most fun?'
 
I think substr() would be quicker than a regex for this.
 
Here's a solution using php's split function..

Code:
<?php
	 $myVal = "1000000001-YZKCGXZS-000000000000";
	 $myValsIndexed = split("-", $myVal);
	 print_r ($myValsIndexed);
	 //or
	 print "<p />";
	 foreach($myValsIndexed as $item){
	 print "$item<br />\n";
	 }
	 ?>

HTH

Great Javascript Resource:
 
DRJ478 has the best sollution to this issue.

Code:
<?php
$regkey = "1000000001-YZKCGXZS-000000000000";

$blah =  explodeReg($regkey);

if (isset($blah))
  {
   echo $blah;
  }
else
  {
    echo "missing regkey!";
  }

// the function
function explodeReg($regkey)
  {
   return explode("-", $regkey);
  }
?>

ps. code not tested!
 
uhm, sorry.. you can not echo $blah

$blah is an array!

eg. to echo it:
Code:
echo $blah[0];
echo $blah[1];
echo $blah[2];

or:
Code:
for ($i = 0; $i > count($blah); $i++)
  {
    echo $blah[$i];
  }

good luck!
 
I prefer to use print_r when printing arrays for debug purposes:
Code:
echo '<pre>';print_r($blah);echo '</pre>';
Makes the output much easier to understand.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top