PHP Notes 7

PHP Notes 7 Lakshman Gupta 9:47 AM 10/8/2019
PHP Tutorial

Regular Expressions
Regular expressions, a powerful tool for manipulating text and data, are found in scripting languages, editors,
programming environments, and specialized tools.
Special characters: "/" "-" "^" "." "\" "^" "$" "*" "+" "(" ")"
Equivalent Description or Meaning Examples
/abc/ search for substring "abc"
/[akt]/ search for one of "a", "k" or "t".
/[^akt]/ true if string do not have any one of "a", "k" or "t".
/[0-4]/ /[01234]/ search for one of "0", "1", "2", "3" or "4"
/.../i Case insensitive
* Zero or more /abck*pqr/
+ One or more /abck+pqr/
\d [0-9] Any decimal digit [0123456789]
\D Any character that is not a decimal digit
\w [0-9A-Za-z_] Alphanumerics and underscore
\W Any "non-word" character
\s [ \r\t\n\f] Any Whitespace character
\S Any character that is not a whitespace character
\b At word boundary (at the beginning or end) /\babc/ or /abc\b/ or /\babc\b/
\r Return (CR)
\f Linefeed (LF)
\n New line (CRLF)
\t Tab
/^.../ Starts with /^klm/
/...$/ Ends with /ali$/ /^good$/i
/\d{4}/ 4 digits /\d\d\d\d/ /[A-Z]{3}\d{3}/
/\d{2,4}/ 2 to 4 digits /\d{2} [A-Z]{1,3} \d{2,4}/
(...) Grouping
Regular Expression Functions in PHP
preg_match: Perform a regular expression match

Examples:
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match ("/php/i", "PHP is the web scripting language of choice."))
{
print "A match was found.";
}
else
{
print "A match was not found.";
}
// The \b in the pattern indicates a word boundary, so only the distinct
// word "web" is matched, and not a word partial like "webbing" or "cobweb"
if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice."))
{
print "A match was found.";
}
else
{
 print "A match was not found.";
}
if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice."))
{
print "A match was found.";
}
else
{
 print "A match was not found.";
}

preg_replace: Perform a regular expression search and replace

Examples:
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
print preg_replace($pattern, $replacement, $string); // PHP Notes PHP Tutorial

Database Handling
Communication with MySQL
PHP and MySQL are often referred to as the "dynamic duo" of dynamic Web scripting. PHP and MySQL work
very well together, in addition to the speed and features of each individual tool.
The steps in using MySQL databases in PHP are as follows:

1. Connect to MySQL Server and select database (usually placed in main of the PHP scripts):
$dblink = mysql_connect("localhost", "db_user", "my_password") or die("Could not connect");
mysql_select_db("my_db") or die("Can not select database");
2. Prepare and submit a query:
$query = "select * from person where p_city = '$city'
order by p_lname";
$qno = mysql_query($query) or die("Query error: <b>$query</b><hr />" . mysql_error());
3. Get number of records selected / affected:
$n = mysql_num_rows ($qno); // Get number of records affected.
4. Fetch data from database server:
while ($record = mysql_fetch_assoc($qno))
{
extract($record); // Create variables by table column names
........ // Use these created variables
}

5. Free the query resources:
mysql_free_result ($qno); // Free query result set.
6. Close connection to database server:
mysql_close($dblink); // Close connection.

MySQL Example

The following is a simple example of how to dump the contents of a MySQL table using PHP. The example assumes you have a MySQL user called "db_user" who can connect with password "my_password" from localhost. In the example below, PHP implements the following procedure:

<?php
// Connect and select database:
$dblink = mysql_connect("localhost", "db_user", "my_password")
or die("Could not connect");
mysql_select_db("my_db") or die("Can not select database");
// Perform SQL query:
$query = "select * from user";
$qno = mysql_query($query)
or die("Query failed: $query<hr />" . mysql_error();
$n = mysql_num_rows ($qno); // Get number of records selected/affected.
// Output results in HTML:
print "<table border='1'>\n";
while ($user = mysql_fetch_assoc($qno)) // While a record is retrieved...
{
// Print column headings if first row
if (!$header_printed)
{
print "\t<tr>\n";
$fields = array_keys ($user);

Popular posts from this blog

Computer Fundamental In Hindi