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 create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Split an alphanumeric string into Array in PHP

Last accessed pages

  1. Simple Admin Login PHP Script (10883)
  2. Send Email with Nodemailer (3165)
  3. Responses (311)
  4. Accessing objects in different Timeline (1522)
  5. XML data from ActionScript to a PHP script (1289)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (309)
  2. Read Excel file data in PHP - PhpExcelReader (113)
  3. The Four Agreements (94)
  4. PHP Unzipper - Extract Zip, Rar Archives (90)
  5. The Mastery of Love (84)
Chat
Chat or leave a message for the other users
Full screenInchide