• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Str_split string into 2 separate arrays

Hi,
I have the code below which works great. It takes a string and inserts <Play>digit from string.wav</Play>

$FromCaller = 1234567890;

$arr = str_split($FromCaller);
$new = '<Play>../audio/callin/' . implode('.wav</Play><Play>../audio/callin/', $arr) . '.wav</Play>';
echo $new;

Open in new window


Ideally what I"m after it to take the same string and create 2 arrays.
The first one being 3 digits long (the area code)
and the second the remaining 7 digits.

So a string like 1234567890

Would output looking like:
<Play>123.wav</Play>
<Play>4.wav</Play><Play>5.wav</Play> and so on...
0
tjyoung
Asked:
tjyoung
  • 2
2 Solutions
 
DerokorianCommented:
$number = '1234567890';

$areacode = substr($number,0,3);
$array = str_split(substr($number,3));

$new = '<Play>../audio/callin/' . $areacode .'.wav</Play><Play>../audio/callin/'. implode('.wav</Play><Play>../audio/callin/', $array) . '.wav</Play>';

echo $new;

Open in new window

0
 
Ray PaseurCommented:
You can use substr() to get the first three digits.
0
 
Ray PaseurCommented:
See if this makes sense...
http://www.laprbass.com/RAY_temp_tjyoung.php
<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);


$num = '1234567890';

// GET THE A/C AND NUMBER
$ac  = substr($num,0,3);
$nbr = substr($num, 3);

// PREPARE THE AC
$ac = '<play>' . $ac . '.wav' . '</play>';

// PREPARE THE NUMBER
$arr = str_split($nbr);
$new = '<play>' . implode('.wav</play><play>', $arr) . '.wav</play>';

// CONCATENATE THE STRINGS
$new = $ac . PHP_EOL . $new;
echo htmlentities($new);

Open in new window

0
 
tjyoungAuthor Commented:
Both are perfect.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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