Solved

Change format of string

Posted on 2011-09-29
7
265 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

22 Experts available now in Live!

Get 1:1 Help Now