Solved

Change format of string

Posted on 2011-09-29
7
264 Views
Last Modified: 2012-05-12
Hi Experts!


How to change this:
$str = "(3) 4305 (41) 5391 5394 (47) 6229 6230 (51) 6771";

Open in new window

into this?:
$str = "4305(03)-5391(41)-5394(41)-6229(47)-6230(47)-6771(51)";

Open in new window



Here's what should happen step by step (it may be different, this is my version):
(all numbers are space-separated)

1)) if there's no number in brackets between two main numbers, insert previous number in brackets between them, example:
before: "(3) 4305 (41) 5391 5394 (47) 6229 6230 (51) 6771"
after: "(3) 4305 (41) 5391 (41) 5394 (47) 6229 (47) 6230 (51) 6771"

2)) insert hyphen before opening bracket:
"-(3) 4305 -(41) 5391 -(41) 5394 -(47) 6229 -(47) 6230 -(51) 6771"

3)) remove spaces (and first hyphen):
"(3)4305-(41)5391-(41)5394-(47)6229-(47)6230-(51)6771"

4)) move number in brackets with brackets to the end of each main number:
"4305(3)-5391(41)-5394(41)-6229(47)-6230(47)-6771(51)"

5)) add '0' (zero) before single digits in brackets:
 "4305(03)-5391(41)-5394(41)-6229(47)-6230(47)-6771(51)"


Thanks for any help.

0
Comment
Question by:Zado
  • 4
  • 3
7 Comments
 
LVL 8

Author Comment

by:Zado
ID: 36816745
Hey guys, if you answer 1st point, would be fantastic:

1)) if there's no number in brackets between two main numbers, insert previous number in brackets between them, example:
before: "(3) 4305 (41) 5391 5394 (47) 6229 6230 (51) 6771"
after: "(3) 4305 (41) 5391 (41) 5394 (47) 6229 (47) 6230 (51) 6771"

I can manage rest.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36817172
Here's what I have for now - if you can wait for a few more moments ...
<?php

$str = "(3) 4305 (41) 5391 5394 (47) 6229 6230 (51) 6771";

// explode the string so we can work with separate numbers
$basearray = explode(" ",$str);

// show me the result
print_r($basearray);

$resultarray = array();

$prev = "number";
// remember the first element
$pos = strpos($basearray[0], "(");
if($pos === false){
    $prev = "number";
}
else{
    $prev = "bracket";
    $lastbracket = $basearray[0];
}

$i = 0;

foreach($basearray as $key => $val){
    $pos = strpos($val, "(");
    
    if($pos === false){
        // so the number is without brackets
        //check for previous one
        
        // we have two numbers without brackets 
        if($prev == "number"){
            $resultarray[$i] = $lastbracket;
            $i++;
            $resultarray[$i] = $val;
        }
        else{
            // previous number is bracket
            $resultarray[$i] = $val;
            $i++;
            
            // this is the number without brackets
            $prev = "number";
        }
        
        $prev = "number";
        
    }
    elseif($pos == 0 && strlen($val) == 3){
    
        // the number is with brackets
        $bracketnumber = substr($val,0,1)."0".substr($val,1,2);
        
        //replace the single number with 0x
        //$basearray[$key] = $bracketnumber;
        
        $resultarray[$i] = $bracketnumber;
        $i++;
        
        $prev = "bracket";
        $lastbracket = $val;        
    }
    else{
    
        // the previous was number or the bracket
        if($prev == "bracket"){
            $resultarray[$i] = $val;
            $i++;
        }
        else{
        
            // no need to fill the bracket number
            $bracketnumber = $val;
            $resultarray[$i] = $bracketnumber;
            $i++;
            
            $prev = "bracket";
            $lastbracket = $val;          
        }
    }
    
    //print_r($resultarray);
    echo"<br />";
}

echo"<br /><br />";
print_r($resultarray);


?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36817241
Is this partly what you need ?

<?php

$str = "(3) 4305 (41) 5391 5394 (47) 6229 6230 (51) 6771";

// explode the string so we can work with separate numbers
$basearray = explode(" ",$str);

// show me the result
print_r($basearray);

$resultarray = array();

$prev = "number";
// remember the first element
$pos = strpos($basearray[0], "(");
if($pos === false){
    $prev = "number";
}
else{
    $prev = "bracket";
    $lastbracket = $basearray[0];
}

$i = 0;

foreach($basearray as $key => $val){
    $pos = strpos($val, "(");
    
    if($pos === false){
        // so the number is without brackets
        //check for previous one
        
        // we have two numbers without brackets 
        if($prev == "number"){
        
            $resultarray[$i] = $lastbracket;
            //echo"$resultarray[$i]<br />";
            $i++;
            $resultarray[$i] = $val;
            //echo"$resultarray[$i]<br />";
            $i++;
        }
        else{
            // previous number is bracket
            $resultarray[$i] = $val;
            //echo"$resultarray[$i]<br />";
            $i++;
            
            // this is the number without brackets
            $prev = "number";
        }
        
        $prev = "number";
        
    }
    elseif($pos == 0 && strlen($val) == 3){
    
        // the number is with brackets
        $bracketnumber = substr($val,0,1)."0".substr($val,1,2);
        
        //replace the single number with 0x
        //$basearray[$key] = $bracketnumber;
        
        $resultarray[$i] = $bracketnumber;
        $i++;
        
        $prev = "bracket";
        $lastbracket = $val;        
    }
    else{
    
        // this one is a bracket > single number
    
        // the previous was number or the bracket
        if($prev == "bracket"){
            //$resultarray[$i] = $val;
            //$i++;
        }
        else{
        
            // no need to fill the bracket number
            $bracketnumber = $val;
            $resultarray[$i] = $bracketnumber;
            $i++;
            
            $prev = "bracket";
            $lastbracket = $val;          
        }
    }
    
    //print_r($resultarray);
    //echo"<br />";
}

echo"<br /><br />";
print_r($resultarray);


?>

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 36817319
A complete one I guess:

<?php

$str = "(3) 4305 (41) 5391 5394 (47) 6229 6230 (51) 6771";

// explode the string so we can work with separate numbers
$basearray = explode(" ",$str);

// show me the result
print_r($basearray);

$resultarray = array();

$prev = "number";
// remember the first element
$pos = strpos($basearray[0], "(");
if($pos === false){
    $prev = "number";
}
else{
    $prev = "bracket";
    $lastbracket = $basearray[0];
}

$i = 0;

foreach($basearray as $key => $val){
    $pos = strpos($val, "(");
    
    if($pos === false){
        // so the number is without brackets
        //check for previous one
        
        // we have two numbers without brackets 
        if($prev == "number"){
        
            $resultarray[$i] = $lastbracket;
            //echo"$resultarray[$i]<br />";
            $i++;
            $resultarray[$i] = $val;
            //echo"$resultarray[$i]<br />";
            $i++;
        }
        else{
            // previous number is bracket
            $resultarray[$i] = $val;
            //echo"$resultarray[$i]<br />";
            $i++;
            
            // this is the number without brackets
            $prev = "number";
        }
        
        $prev = "number";
        
    }
    elseif($pos == 0 && strlen($val) == 3){
    
        // the number is with brackets
        $bracketnumber = substr($val,0,1)."0".substr($val,1,2);
        
        //replace the single number with 0x
        //$basearray[$key] = $bracketnumber;
        
        $resultarray[$i] = $bracketnumber;
        $i++;
        
        $prev = "bracket";
        $lastbracket = $val;        
    }
    else{
    
        // this one is a bracket > single number
    
        // the previous was number or the bracket
        if($prev == "bracket"){
            //$resultarray[$i] = $val;
            //$i++;
        }
        else{
        
            // no need to fill the bracket number
            $bracketnumber = $val;
            $resultarray[$i] = $bracketnumber;
            $i++;
            
            $prev = "bracket";
            $lastbracket = $val;          
        }
    }
    
    //print_r($resultarray);
    //echo"<br />";
}

$r = "";

for($i=1;$i<count($resultarray);$i=$i+2){
    $r .= $resultarray[$i].$resultarray[$i-1]."-";
}

echo"<br /><br />";
$r = trim($r,"-");
echo $r;


?>

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 36890682
This is excellent!
Except one tiny thing: when we've got situation like this one: "(4) 4716 4717", the output is "4716(04)-4717(4)", so "0" is added just to first number in brackets, not to the second one.
0
 
LVL 8

Author Comment

by:Zado
ID: 36891503
It's ok, I simply added
$r = preg_replace("/\(([0-9])\)/","(0$1)",$r);

Open in new window

at the end of your script.

Thanks for your help! :-)
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36892917
Glad it works, thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now