Debt Consolidation - Web Hosting - Car Insurance Quotes - Credit Cards - Chants

PDA

View Full Version : PHP Help..


BAM78
Aug 11th 2008, 9:38 pm
I have a script that outputs my data like this

YH1H-BJ-YD35-0Y12Y8-102Y0Y

Each output is random

How can I make that look like this

YH1H/-BJ-/YD35/-0Y12/Y8-/102Y/0Y

sarahk
Aug 11th 2008, 9:48 pm
There's probably a regular expression that will do it easily but you can always use explode('-',$str) to get the bits and then just rebuild it from the array.

BAM78
Aug 11th 2008, 9:58 pm
I need the / in the same spot
and all the outputted data is the same length

zac439
Aug 11th 2008, 10:21 pm
We are going to need more information. One would think that all dashes have a slash preceding them or superceding them.

Was this a mistake or intentional:
YH1H/-BJ-/YD35/-0Y12/Y8-/102Y/0Y


(I don't see a dash- in which case if it was intentional we need to know how we find out where slashes go before we can manipulate the original string)

BAM78
Aug 12th 2008, 4:36 am
YH1H/-BJ-/YD35/-0Y12/Y8-/102Y/0Y
was intentional

I need the / in the same spots as this example for all the output data

example of output data
AH1H-BJ-YD35-0Y12Y8-102Y0Y
BH1H-BJ-HD35-0Y1HY8-102H0U


example of what I need
AH1H/-BJ-/YD35/-0Y12/Y8-/102Y/0Y
BH1H/-BJ-/HD35/-0Y1H/Y8-/102H/0U

mallorcahp
Aug 12th 2008, 4:55 am
if the "/" is always in the same place you could use substr ...


$mystr = "AH1H-BJ-YD35-0Y12Y8-102Y0Y";

$newstr .= substr($mystr,0,4) . "/";
$newstr .= substr($mystr,4,4) . "/";
$newstr .= substr($mystr,8,4) . "/";
$newstr .= substr($mystr,12,5) . "/";
$newstr .= substr($mystr,17,3) . "/";
$newstr .= substr($mystr,20,4) . "/";
$newstr .= substr($mystr,24,2);

echo $newstr;

is0is0
Aug 12th 2008, 6:28 am
Your dashes are random and not following a pattern therefore I cannot help you unless you want to do regex.

zac439
Aug 12th 2008, 8:19 am
Your dashes are random and not following a pattern therefore I cannot help you unless you want to do regex.

Exactly what I tried to tell him :rolleyes:

sarahk
Aug 12th 2008, 12:15 pm
A better way to build the string is to use implode() on the array generated by explode.