Php-mysql Course

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: https://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',
  )
)

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag can be used to create input text field in web page?
<form> <input> <div>
<input type="text" name="a_name" value="val" />
Which CSS property displays the text in a small-caps font?
display font-variant font-style
h3 {
  font-variant: small-caps;
}
What instruction displays a notice box with a message inside it, in JavaScript?
for() Date() alert()
var msg = "Visit CoursesWeb.net";
alert(msg);
Indicate the PHP code used to get the users IP.
$_SERVER["HTTP_USER_AGENT"] $_SERVER["REMOTE_ADDR"] $_GET[]
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
Split an alphanumeric string into Array in PHP

Last accessed pages

  1. CSS cursor property - Custom Cursors (6300)
  2. Adobe Flash Courses ActionScript 3 Tutorials (6680)
  3. A simple script ActionScript 3 (4497)
  4. CSS Rhombus Shape (7685)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (143505)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (738)
  2. CSS cursor property - Custom Cursors (93)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (65)
  4. Read Excel file data in PHP - PhpExcelReader (65)
  5. The Mastery of Love (63)