Jump to content

exploding help


ccrevcypsys

Recommended Posts

How would i be able to explode a phone number that looks like this

(775)443-9864

 

this is what i have

 

<?php 
$Number = $mainQuery[0]['OfficePhone'];
$exploded = explode("-", $Number);
?>
                	(<input type="text" name="Areacode" style="width:25px;" value="<?php echo $exploded[0]; ?>" />) <input type="text" style="width:25px;" name="FirstSet" value="<?php echo $exploded[1]; ?>" />-<input type="text" style="width:30px;" name="SecSet" value="<?php echo $exploded[2]; ?>" />

Link to comment
https://forums.phpfreaks.com/topic/81716-exploding-help/
Share on other sites

I wouldn't explode it.  Instead I would do something like this:

 

$input = "(123) 456-7890";
$replace = array("(",")"," ","-");
$clean = str_replace($replace,"",$input); // yielding 1234567890
$areacode = substr($input,0,3); // yielding the area code (123)
$next_three = substr($input,3,3); // yielding first three digits (456)
$last_four = substr($input,7,4); // yielding last four (7890)

 

This will remove all instances of "(" or ")" or " " or "-" with nothing, yielding a clean str of integers.

Link to comment
https://forums.phpfreaks.com/topic/81716-exploding-help/#findComment-415030
Share on other sites

Try this.  It breaks the area code, next three numbers and last four numbers into a separate variable.

<?
$date = "(775)443-9864";

$d = explode("-", $date); //get rid of the dash

echo $d[0]; //output (775)443
echo "<br>";
echo $d[1]; //output 9864
echo "<br>";

$v = $d[0]; //assigns (775)443 to another variable to explode next
$t = explode("(", $v); //get rid of the first (
echo "<br>";
echo $t[1]; //output 775)443

$s = $t[1]; //assigns 775)443 to another variable to explode next
$a = explode(")", $s); //get rid of the first )
echo "<br>";
print_r($a); //output $a[0] = 775 and $a[1] = 443

//then you can combine the variables to any format you need with implode
//hope this is what you needed.  Lots of steps. 

?>

Link to comment
https://forums.phpfreaks.com/topic/81716-exploding-help/#findComment-415033
Share on other sites

well i figured a way out that isnt like 1000 lines of code and here it is incase anyone needs it

<?php 
$OfficePhone = ereg_replace(') ',')-',$mainQuery[0]['OfficePhone']);
$exploded = explode("-", $OfficePhone);
?>

It replaces the ') ' with ')-' so taht when i explode it the () are still in tact for the area code. But thanks guys.

 

Hows that for ezy lol lol

Link to comment
https://forums.phpfreaks.com/topic/81716-exploding-help/#findComment-415035
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.