Friday 26 June 2015

Split an alphanumeric string into Array in PHP

The function presented in this page can be used in PHP to split an alphanumeric string (a string with multiple pairs of sub-strings with: "Name Number", separated by certain characters).
This function separates the "Name" and "Number", and adds them into two separated arrays, in their order from string.
- See the examples and the comments in code.

Code of the function

/*
 receives 3 arguments:
 - $str = the alphanumeric string,
 - $dlm1 = the character that separates the Name by Number
 - $dlm2 = the character that separates the pairs of sub-strings: "Name Number"

 Something like this:
     "Name[$dlm1]Number[$dlm2]Name[$dlm1]Number"
  - the delimiter character ($dlm1, and $dlm2) can be any character, for example: space character, dash, dot, comma, :, etc.
 returns an 2 dimensional array with Names and Numbers from $str, separated in 2 arrays, in their order in string
*/
function splitNameNumber($str, $dlm1, $dlm2) {
  // PHP-MySQL course: http://coursesweb.net/php-mysql/
  // array with characters that must be escaped in RegExp pattern
  $chrs = array('.','(',')','[',']','<','>','?',"'",'^','*','-','+','\\','/');
  if(in_array($dlm1, $chrs)) $dlm1 = '\\'.$dlm1;
  if(in_array($dlm2, $chrs)) $dlm2 = '\\'.$dlm2;

  // gets an array with the 'Name-Number' sub-strings from $str
  if(preg_match_all('/([a-z \._\-'. $dlm1 . $dlm2. ']+[0-9]+)/i', $str, $mt)) {
    $re = array();              // variable for data to return
    $nrmt = count($mt[0]);      // number of matched substrings

    // traverse the matched sub-strings
    for($i=0; $i<$nrmt; $i++) {
      // gets separated the Name and Number, and adds them in $re array
      if(preg_match('/([a-z \._\-'. $dlm1. ']+)([0-9]+)/i', $mt[0][$i], $mt2)) {
        $re['name'][$i] = trim($mt2[1], ' '. $dlm1);
        $re['num'][$i] = $mt2[2];
      }
    }

    return $re;
  }
  else return false;
}
- Example:
<?php
// HERE add the splitNameNumber() function

// string with Name and Number separated by dot '.', and the sub-strings separated by comma ','
$str1 = 'PHP-MySQL Course.2013, CoursesWeb.net.2011, MarPlo.net.2009';
$split_str1 = splitNameNumber($str1, '.', ',');

// text with Name and Number separated by space, and the sub-strings separated by comma ','
$str2 = 'Code Snippets 12, PHP code 2011, MarPlo.net Courses 2009';
$split_str2 = splitNameNumber($str2, ' ', ',');

// string with Name and Number, and the sub-strings separated by space
$str3 = 'PHP_MySQL Tutorials 23 Ajax lessons 9 Web Development 2013';
$split_str3 = splitNameNumber($str3, ' ', ',');

// test
echo "<pre>// From \$str1 \n";
var_export($split_str1);

echo "\n// From \$str2 \n";
var_export($split_str2);

echo "\n// From \$str3 \n";
var_export($split_str3);
echo '</pre>';
Results:
// From $str1 
array (
  'name' => array (
    0 => 'PHP-MySQL Course',
    1 => 'CoursesWeb.net',
    2 => 'MarPlo.net',
  ),
  'num' => array (
    0 => '2013',
    1 => '2011',
    2 => '2009',
  )
)
// From $str2 
array (
  'name' => array (
    0 => 'Code Snippets',
    1 => 'PHP code',
    2 => 'MarPlo.net Courses',
  ),
  'num' => array (
    0 => '12',
    1 => '2011',
    2 => '2009',
  )
)
// From $str3 
array (
  'name' => array (
    0 => 'PHP_MySQL Tutorials',
    1 => 'Ajax lessons',
    2 => 'Web Development',
  ),
  'num' => array (
    0 => '23',
    1 => '9',
    2 => '2013',
  )
)

0 comments:

Post a Comment