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 adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Split an alphanumeric string into Array in PHP

Last accessed pages

  1. The Mastery of Love (7455)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141840)
  3. Convert BBCode to HTML and HTML to BBCode with JavaScript (9405)
  4. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26337)
  5. Read Excel file data in PHP - PhpExcelReader (97361)

Popular pages this month

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