PHP Web Concepts
PHP Web Concepts
<html> </select>
<body> <input type="submit" />
<p>Choose a site to visit :</p> </form>
</body>
<form action="<?php $_PHP_SELF ?>" </html>
method="POST"> <select
name="location">
PHP ─ Sending Emails
• The user clicks the browse button and selects a file to upload from the
local PC.
• The full path to the selected file appears in the text field, then the user
clicks the submit button.
• The PHP script that was specified as the form handler in the form's
action attribute checks that the file has arrived and then copies the file
into an intended directory.
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Function Description
Function Description
Expression Description
$regex = "colou{0,1}r";
Since the "?" metacharacter makes the one character that it follows
optional, the regular expression finds a match with or without
the "u".
Shorthand characters
• We have shortcuts for some of the most
commonly used character sets.
Searches for a pattern that matches white space, a word character, another
white space, and than 4 digits.
Let's test the expression with the following code:
Result:
We found a match to the expression: go
• preg_match searches for a string that matches the regular expression
($regex) in the string ($string), and the first match it finds is stored as
the first item of the $match array.
The i modifier
• Regular expressions are by default case sensitive.
The regular expressions /chr*/ is different from /Chr*/, because the the
first expression starts with a lowercase 'c' while the second starts with
a capital 'C'.
But we can change this behavior by adding the i modifier, right after the
closing delimiter of the regular expression.
So, the regular expression:
$regex = "/Chr*/i"; will match both 'Chrome' as well as 'chrome'.
Global regular expression matching (preg_match_all)
Example :
Result:
Array
(
[0] => reg
[1] => reg
[2] => reg
)
Search and replace with preg_replace
• In order to replace strings, we use the preg_replace() function, with the
following syntax:
$regex = "/miss?pp?ell?e?d/";
$replace = "misspelled";
$string = "He mispeled the word in all of his emails.";
echo preg_replace($regex, $replace, $string);
Result is:
He misspelled the word in all of his emails.
split strings by regular expressions(preg_split )
preg_split is the built-in PHP function that we use when we want to split a string by regular
expression. It has the following syntax:
preg_split($regex, $string);
$regex - the expression that we search for, and want to split at.
$string - the string in which we search for the expression.
In the following example we want to split at the comma followed by any number of spaces.
$regex = "/,\s+/";
$string = "html, css, javascript, php";
$languages = preg_split($regex, $string);
print_r($languages);
Result is:
Array
(
[0] => html
[1] => css
[2] => javascript
[3] => php
)
search for matches inside arrays(preg_grep)
preg_grep searches for matches inside arrays, and brings back an array that is
consisted only of matching items.
The syntax:
preg_grep($pattern, $array);
$array stands for the array in which we search for matching items.
In the following example, we search inside the $cars array for items that start with
't' (lower or upper case):
Result is:
Array
(
[1] => Tesla
[3] => toyota
)