Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Change format of string

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
Zado
Asked:
Zado
  • 4
  • 3
1 Solution
 
ZadoAuthor Commented:
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
 
Lukasz ChmielewskiCommented:
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
 
Lukasz ChmielewskiCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Lukasz ChmielewskiCommented:
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
 
ZadoAuthor Commented:
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
 
ZadoAuthor Commented:
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
 
Lukasz ChmielewskiCommented:
Glad it works, thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now