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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.