AWT Chapter 2 AdvancePHPpptx 2023 08 07 11 54 02
AWT Chapter 2 AdvancePHPpptx 2023 08 07 11 54 02
Unit – 2
Advance PHP
• Regular Expression
• Mail function
• Many available modern languages and tools apply regexes on very large
files and strings.
• When you search for data in a text, you can use this search pattern to
describe what you are searching for.
• Here / is the delimiter, MU is the pattern that is being searched for, and i
is a modifier that makes the search case-insensitive.
• The delimiter Can be any character that is Not a letter, number, backslash
or space.
• With the help of in-built regexes functions, easy and simple solutions are
provided for identifying patterns.
• It helps in identifying specific template tags and replacing those data with
the actual data as per the requirement.
• Regexes are very useful for checking password strength and form
validations.
REGULAR EXPRESSION
Two types of regular expression functions
• (This feature was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0).
POSIX - Portable Operating System Interface for uniX
• https://www.php.net/manual/en/book.regex.php
• https://www.php.net/manual/en/book.pcre.php
REGULAR EXPRESSION
POSIX Regex Functions
[^] It finds the items which are not in range for example [^abc] means NOT a, b or c.
– (dash) It finds for character range within the given item range for example [a-z] means a through z.
SPECIAL
MEANING
CHARACTER
\n It denotes a new line.
\r It denotes a carriage return.
\t It denotes a tab.
\v It denotes a vertical tab.
\f It denotes a form feed.
\xxx It denotes octal character.
\xhh It denotes hex character.
REGULAR EXPRESSION
There is a difference between strings inside single quotes and strings inside
double quotes in PHP.
The former are treated literally, whereas for the strings inside double-quotes
means the content of the variable is printed instead of just printing their
names.
REGULAR EXPRESSION
Function Description
Returns the number of times the pattern was found in the string, which
preg_match_all()
may also be 0
1. preg_match( )
Output
1
REGULAR EXPRESSION
preg_match( )
Parameter Description
Optional. The variable used in this parameter will be populated with an array containing all of the matches
matches
that were found
• Optional. A set of options that change how the matches array is structured: PREG_OFFSET_CAPTURE
- When this option is enabled, each match, instead of being a string, will be an array where the first
element is a substring containing the match and the second element is the position of the first
flags
character of the substring in the input.
• PREG_UNMATCHED_AS_NULL - When this option is enabled, unmatched sub patterns will be returned
as NULL instead of as an empty string.
Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will
offset
not find matches that occur before the position given in this parameter
REGULAR EXPRESSION
preg_match( ) Example-2
<?php
$str = "Welcome to MU for the visit.";
$pattern = "/mu/i";
echo preg_match($pattern, $str);
echo "</br>";
preg_match($pattern, $str, $matches);
print_r($matches);
echo "</br>";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
Output:
1
Array ( [0] => MU )
Array ( [0] => Array ( [0] => MU [1] => 11 ) )
REGULAR EXPRESSION
2. preg_match_all()
This function will tell you how many matches were found for a pattern in a string.
<?php
$str = "Visit MU to know MU and also to see MU.";
$pattern = "/MU/i";
echo preg_match_all($pattern, $str);
?>
// Outputs 3
REGULAR EXPRESSION
2. preg_match_all()
Parameter Description
• Optional. A set of options that change how the matches array is structured.
flags • PREG_SET_ORDER - Each element in the matches array contains matches of all groupings for one of the found matches in
the string.
• Any number of the following options may be applied: PREG_OFFSET_CAPTURE - When this option is enabled, each match,
instead of being a string, will be an array where the first element is a substring containing the match and the second element is
the position of the first character of the substring in the input.
• PREG_UNMATCHED_AS_NULL - When this option is enabled, unmatched subpatterns will be returned as NULL instead of as
an empty string.
Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that
offset
occur before the position given in this parameter
REGULAR EXPRESSION
3. preg_replace( )
This function will replace all of the matches of the pattern in a string with another string based on
pattern/expression.
<?php
$str = "Visit MU to know MU.";
$pattern = "/MU/i";
echo $str;
echo "<br/>";
echo preg_replace($pattern, "Marwadi University", $str);
?>
// Outputs
Visit MU to know MU.
Visit Marwadi University to know Marwadi University.
REGULAR EXPRESSION
3. preg_replace( )
Parameter Description
4. preg_split( )
The preg_split() function breaks a string into an array using matches of a regular expression as
separators.
<?php
$date = "1970-01-01 00:00:00";
$pattern = "/[-\s:]/";
$components = preg_split($pattern, $date);
print_r($components);
?>
Output:
Array
(
[0] => 1970
[1] => 01
[2] => 01
[3] => 00
[4] => 00
[5] => 00
)
Note: The regular expression \s is a predefined character class. It indicates a single whitespace character.
REGULAR EXPRESSION
4. preg_split( )
REGULAR EXPRESSION
4. preg_split( ) – Example – 2 Output:
Array
(
[0] => 1970
Using the PREG_SPLIT_DELIM_CAPTURE flag:
[1] => -
[2] => 01
<?php
[3] => -
$date = "1970-01-01 00:00:00";
[4] => 01
$pattern = "/([-\s:])/";
[5] =>
$components = preg_split($pattern, $date, -1,
[6] => 00
PREG_SPLIT_DELIM_CAPTURE);
[7] => :
print_r($components);
[8] => 00
?>
[9] => :
[10] => 00
)
REGULAR EXPRESSION
5. preg_quote( )
preg_quote() takes string and puts a backslash in front of every character that is part of the regular
expression syntax.
<?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords;
?>
Output:
\$40 for a g3\/400
REGULAR EXPRESSION
5. preg_quote( )
The preg_quote() function adds a backslash to characters that have a special meaning in regular
expressions so that searches for the literal characters can be done.
<?php
$search = preg_quote(“://”, ”/”);
echo $search;
echo “</br>”;
$input = 'https://www.w3schools.com/';
$pattern = "/$search/";
if(preg_match($pattern, $input))
{
echo "The input is a URL.";
}
else
{
echo "The input is not a URL.";
}
?>
Output:
\:\/\/
The input is a URL.
REGULAR EXPRESSION
6. preg_grep()
The preg_grep( ) function returns an array containing only elements from the input that match the
given pattern.
<?php
?>
Output:
Array
(
[1] => Pink
[4] => Purple
)
METACHARACTERS
Meta characters
• There are two kinds of characters that are used in regular expressions
these are: Regular characters and Meta characters.
QUANTIFIER MEANING
a+ It matches the string containing at least one a.
a* It matches the string containing zero or more a’s.
a? It matches any string containing zero or one a’s.
a{x} It matches letter ‘a’ exactly x times .
a{2, 3} It matches any string containing the occurrence of two or three a’s.
a{2, } It matches any string containing the occurrence of at least two a’s.
METACHARACTERS
QUANTIFIER MEANING
a{2} It matches any string containing the occurrence of exactly two a’s.
It matches any string containing the occurrence of not more than y
a{, y}
number of a’s.
a$ It matches any string with ‘a’ at the end of it.
• cURL library is used to communicate with other servers with the help of a
wide range of protocols.
• cURL allows the user to send and receive the data through the URL
syntax.
Authentication
Use of cookies
WEB SCRAPING USING cURL
How Does the cURL work?
cURL works by sending a request to a web site, and this process includes the
following four parts:
• Initialization
• Setting the options
• Execution with curl_exec
• Releasing the curl_handle
WEB SCRAPING USING cURL
How Does the cURL work?
WEB SCRAPING USING cURL
Some basic cURL functions:
• The curl_init() function will initialize a new session and return a cURL handle.
• curl_exec($ch) function should be called after initialize a cURL session and all the options for the session
are set. Its purpose is simply to execute the predefined CURL session (given by ch).
• curl_setopt($ch, option, value) set an option for a cURL session identified by the ch parameter. Option
specifies which option is to set, and value specifies the value for the given option.
• curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) return page contents. If set to false then no output
will be returned.
• curl_setopt($ch, CURLOPT_URL, $url) pass URL as a parameter. This is your target server website address.
This is the URL you want to get from the internet.
• curl_exec($ch) grab URL and pass it to the variable for showing output.
cURL works by sending a request to a web site, and this process includes the
following four parts:
• Initialization
• Setting the options
• Execution with curl_exec
• Releasing the curl_handle
WEB SCRAPING USING cURL
1. How to download the contents of a remote website to a local
file?
<?php
$handle=curl_init();
$url = "https://www.imdb.com/";
curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($handle, CURLOPT_URL,$url);
$output =curl_exec($handle);
echo $output;
curl_close($handle);
?>
WEB SCRAPING USING cURL
2.How to download a file from a remote site using cURL?
<?php
$url = "https://www.mprnews.org/story/2015/05/15/books-short-stories";
curl_close($handle);
fclose($fileHandle);
?>
WEB SCRAPING USING cURL
3. How to handle cookies with cURL?
<?php
$handle = curl_init();
$url="https://www.marwadiuniversity.ac.in/";
$file = __DIR__ . DIRECTORY_SEPARATOR . "cookie.txt";
curl_setopt_array($handle,array(CURLOPT_URL => $url,
CURLOPT_COOKIEFILE => $file,
CURLOPT_COOKIEJAR => $file,
CURLOPT_RETURNTRANSFER=>true)
);
$data = curl_exec($handle);
curl_close($handle);
?>
MAIL FUNCTION
The mail( ) function allows you to send emails directly from a script.
For the mail functions to be available, PHP requires an installed and working email
system. The program to be used is defined by the configuration settings in the php.ini
file.
Parameter Values
• Message : Required. Defines the message to be sent. Each line should be separated with a
(\n). Lines should not exceed 70 characters.
• headers: Optional. Specifies additional headers, like From, Cc, and Bcc. The additional
headers should be separated with a CRLF (\n).
MAIL FUNCTION
How To Send Mail From Localhost XAMPP Using Gmail
› SMTP=smtp.gmail.com
› smtp_port=587
› smtp_server=smtp.gmail.com
› smtp_port=587
› error_logfile=error.log
› debug_logfile=debug.log
<?php
$to_email = "[email protected]";
$subject = "Test email to send from XAMPP";
$body = "Hi, This is test mail to check how to send mail from Localhost Using Gmail ";
$headers = "From: sender email";
• Web services are open standard (XML, SOAP, HTTP, etc.) based web applications
that interact with other web applications for the purpose of exchanging data.
• It uses a standardized XML messaging system and is not tied to any one operating
system or programming language.
• For example, a client invokes a web service by sending an XML message, then waits
for a corresponding XML response. As all communication is in XML, web services are
not tied to any one operating system or programming language — Java can talk
with Perl; Windows applications can talk with Unix applications.
WEB SERVICES
Components of Web Services
• The basic web services platform is XML + HTTP. All the standard web services work
using the following components −
• The service provider publishes the service in a Service Registry, which can
be used for service requesters in public
• The service requester will look for services, which it needs and retracts
service information.
XML
• Developed by W3C
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
BASIC WEB SERVICES STANDARDS
XML Example
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
BASIC WEB SERVICES STANDARDS
SOAP
• SOAP stands for Simple Object Access Protocol
<soap:Header>
... ...
</soap:Header>
<soap:Body>
... ...
</soap:Body>
</soap:Envelope>
BASIC WEB SERVICES STANDARDS
WSDL
• WSDL stands for Web Services Description Language
• It specifies the location of the service and the operations the services
exposes.