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 is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Split an alphanumeric string into Array in PHP

Last accessed pages

  1. RegExp - Regular Expressions in ActionScript (4713)
  2. JavaScript strip_tags and stripslashes (8829)
  3. Complex Shapes with a single DIV and CSS (3711)
  4. Detecting events in ActionScript 3 (1412)
  5. PHP Image with text on New Lines (2517)

Popular pages this month

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