Solved

Change format of string

Posted on 2011-09-29
7
268 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 dynamically set the form action using jQuery.

730 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