Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change format of string

Posted on 2011-09-29
7
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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