SlideShare a Scribd company logo
PHP -  Introduction to  String Handling
Introduction toIntroduction to
STRINGSTRING
HandlingHandling
StringStringlerler
$a = trim($name); //kırpma
$a = nl2br(“line1nline2”); // “line1 br line2”< > çevirir
printf(“total %d”, 15); // prints to stdout
Strings in PHPStrings in PHP
A string is an array of character.
$a = strtoupper($name); // büyük harf
$a = strtolower($name); // küçük harf
$a = ucfirst($name); // İlk karakterbüyük
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "n", 1);
$a = crpyt($a); // şifreleme
$a = decrypt(encrpyt($a)); // 2-way encription
with Mcrpt extension
Strings in PHPStrings in PHP
Slash ekler- (veritabanına eklerken)
$a = AddSlashes($typedText);
Slashları kaldırır
$a = StripSlashes($typedText);
StringString birleştirme vebirleştirme ve
ayırmaayırma
?<
$pizza = "piece1 piece2 piece3 piece4 piece5";
$pieces = explode (" ", $pizza); // split string into pieces
for($i=0; $i count($pieces); $i++)<
echo "- $pieces[$i] br ";> < >
echo implode(“:", $pieces); // join strings using “:“
?>
Stringleri ayırmaStringleri ayırma
$string = "This is an example string";
$tok = strtok ($string," ");
while ($tok) {
echo "Word=$tok<br>";
$tok = strtok (" ");
}
Strings in PHPStrings in PHP
• string substr (string string, int start [, int length])
• int strlen (string str)
• int strcmp (string str1, string str2) Returns
o < 0 if str1 is less than str2;
o > 0 if str1 is greater than str2,
o 0 if they are equal.
Regular ExpressionsRegular Expressions
• A way of describing a pattern in string
• Use special characters to indicate meta-meaning in
addition to exact matching.
• More powerful than exact matching
• There are 2 sets of function on regular expressions in
PHP
o Functions using POSIX-type reg expr
o Functions using Perl-type reg expr
Regular ExpressionsRegular Expressions
“.” tek bir karakterle eşleşir
.at == “cat”, “sat”, etc.
[a-zA-Z0-9] tek bir karakterle (a-zA-Z0-9)
arasında eşleşir.
[^0-9] rakam olmayan birşeyle eşleşir.
Regular Expr: Built-inRegular Expr: Built-in
Char-setsChar-sets
• [[:alphanum:]] --harf
• [[:digit:]] rakamla
• [[:space:]] boşlukla
Regular ExprRegular Expr
• . Tek karakter
• + 1 ya da daha fazla bulunan stringle
• * 0 ya da daha fazla bulunan stringle
• [a-z] karakter
• ^ değil anlamında
• $ string sonu
• | or
•  özel karakterleri atlar
• (sub-expr) -- sub-expression
• (sub-expr){i,j} i min i, max j ile sub-expr olma durumu
Reg Expr Functions (Perl)Reg Expr Functions (Perl)
• preg_match — Perform a reg expr match
• preg_match_all — Perform a global r.e. match
• preg_replace — Perform a re search & replace
• preg_split — Split string by a reg expr
preg_match -- Perform apreg_match -- Perform a
re matchre match
int preg_match (string pattern, string subject [, array
matches])
• Searches subject for a match to the reg expr given
in pattern.
• Return one match for each subpattern () only
• $matches[0]: the text matching the full pattern
• $matches[1]: the text that matched the first
captured parenthesized subpattern, and so on.
• Returns true if a match for pattern was found
preg_match -- Perform apreg_match -- Perform a
re matchre match
preg_match("/pattern/modifier", subject, array)
Modifiers:
• i: case insensitive search
• m: by default subject is treated single-line even if it contains
newlines, m makes PCRE treat subject multiline (for ^, $)
• s: makes . metacharacter match n
• x: whitespace in pattern is ignored
• E: $ matches only at the end of subject
• U: behave ungreedy (comert)
preg_match -- Perform apreg_match -- Perform a
re matchre match$s = <<<STR
<table><tr><td>cell1</td><td>cell2</td></tr>
<tr><td>cell3</td><td>cell4</td></tr></table>
STR;
preg_match("/<table>(.*)</table>/Us", $s, $r)
// anything between <table> and </table>
preg_match("/<tr><td>(.*)</td><td>(.*)</td></tr>/Us", $r[1],
$t)
// matches cell1 and cell2
preg_match("/<tr>(.*)</tr>/Us", $r[1], $t);
// matches <td>cell1</td><td>cell2</td>
preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch
int preg_match_all (string pattern, string subject, array
matches [, int order])
• Searches subject for all matches and puts them in
matches in the order specified by order.
• After the first match, the subsequent ones are
continued on from end of the last match.
• $matches[0] is an array of full pattern matches
• $matches[1] is an array of strings matched by the
first parenthesized subpattern, and so on.
preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch
preg_match("/<table>(.*)</table>/Us", $s, $r);
preg_match_all("/<tr><td>(.*)</td><td>(.*)</td></
tr>/Us", $r[1], $t);
echo $t[1][0],$t[1][1],$t[2][0],$t[2][1];
// prints cell1cell3cell2cell4
preg_match_all("/<tr><td>(.*)</td><td>(.*)</td></
tr>/Us", $r[1], $t, PREG_SET_ORDER );
//Orders results so that $matches[0] is an array of first
set of matches, $matches[1] is an array of second
set of matches,…
echo $t[0][1],$t[0][2],$t[1][1],$t[1][2];
// returns cell1cell2cell3cell4
preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch
Back reference
preg_match_all ("/(<([w]+)[^>]*>)(.*)(</2>)/",
$html, $matches);
// 2 means [w]+
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", $html, $m)
// return text in html tags
Convert HTML to TextConvert HTML to Text
$html = file(“http://www.page.com”);
$search = array ("'<script[^>]*?>.*?</script>'si",
"'<[/!]*?[^<>]*?>'si",
"'([rn])[s]+'",
"'&(quote|#34);'i",
"'&(amp|#38);'i", …);
$replace = array ("", "", "1", """, "&", …);
$text = preg_replace ($search, $replace, $html);
Reg Expr FunctionsReg Expr Functions
(POSIX)(POSIX)
• ereg (string pattern, string string [, array regs])
o Searches a string for matches to the regular expression given in pattern.
• ereg_replace (string pattern, string subs, string string)
o Scans string for matches to pattern, then replaces the matched text with
subs.
• array split (string pattern, string string [, int limit])
o Split string using pattern
Regular ExprRegular Expr
ereg ("abc", $string);
ereg ("^abc", $string);
ereg ("abc$", $string);
eregi ("(ozilla.[23]|MSIE.3)", $HTTP_USER_AGENT);
ereg ("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)",
$string,$regs);
$string = ereg_replace ("^", "<BR>", $string);
$string = ereg_replace ("$", "<BR>", $string);
$string = ereg_replace ("n", "", $string);
Regular ExprRegular Expr
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date,
$regs)) {
echo "$regs[3].$regs[2].$regs[1]";
}else{
echo "Invalid date format: $date";
}
Regular ExprRegular Expr
$string = "This is a test";
echo ereg_replace (" is", " was", $string);
echo ereg_replace ("( )is", "1was", $string);
echo ereg_replace ("(( )is)", "2was", $string);
Regular ExprRegular Expr
$date = "04/30/1973";
// Delimiters may be slash, dot, or hyphen list
($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year";
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/php-classes-in-
mumbai.html

More Related Content

What's hot (20)

PDF
Perl 6 for Concurrency and Parallel Computing
Andrew Shitov
 
PPTX
Switching from java to groovy
Paul Woods
 
PPTX
Python Workshop
Assem CHELLI
 
PPT
Php String And Regular Expressions
mussawir20
 
PDF
Perl 6 by example
Andrew Shitov
 
PDF
Ruby cheat sheet
Tharcius Silva
 
PPTX
Php Basics Iterations, looping
Muthuganesh S
 
KEY
Achieving Parsing Sanity In Erlang
Sean Cribbs
 
PPTX
Learn python - for beginners - part-2
RajKumar Rampelli
 
PPTX
Five
Łukasz Langa
 
PDF
Perl6 one-liners
Andrew Shitov
 
PDF
Perl6 in-production
Andrew Shitov
 
PPTX
php string part 3
monikadeshmane
 
PDF
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
PDF
Practical approach to perl day2
Rakesh Mukundan
 
PPTX
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
PDF
Introdução ao Perl 6
garux
 
PPTX
Gigigo Ruby Workshop
Alex Rupérez
 
Perl 6 for Concurrency and Parallel Computing
Andrew Shitov
 
Switching from java to groovy
Paul Woods
 
Python Workshop
Assem CHELLI
 
Php String And Regular Expressions
mussawir20
 
Perl 6 by example
Andrew Shitov
 
Ruby cheat sheet
Tharcius Silva
 
Php Basics Iterations, looping
Muthuganesh S
 
Achieving Parsing Sanity In Erlang
Sean Cribbs
 
Learn python - for beginners - part-2
RajKumar Rampelli
 
Perl6 one-liners
Andrew Shitov
 
Perl6 in-production
Andrew Shitov
 
php string part 3
monikadeshmane
 
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Practical approach to perl day2
Rakesh Mukundan
 
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
Introdução ao Perl 6
garux
 
Gigigo Ruby Workshop
Alex Rupérez
 

Viewers also liked (15)

PPT
Php Using Arrays
mussawir20
 
PPT
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
PPT
Chapter 02 php basic syntax
Dhani Ahmad
 
PPT
PHP
sometech
 
PPTX
PHP
Steve Fort
 
PPT
PHP variables
Siddique Ibrahim
 
PPTX
Php string function
Ravi Bhadauria
 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PPSX
Php string
argusacademy
 
PPSX
Php and MySQL
Tiji Thomas
 
PPTX
Cookie and session
Aashish Ghale
 
PPT
Cookies and sessions
Lena Petsenchuk
 
PPTX
PHP slides
Farzad Wadia
 
PDF
Php tutorial
Niit
 
PPT
Introduction to PHP
Jussi Pohjolainen
 
Php Using Arrays
mussawir20
 
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Chapter 02 php basic syntax
Dhani Ahmad
 
PHP variables
Siddique Ibrahim
 
Php string function
Ravi Bhadauria
 
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Php string
argusacademy
 
Php and MySQL
Tiji Thomas
 
Cookie and session
Aashish Ghale
 
Cookies and sessions
Lena Petsenchuk
 
PHP slides
Farzad Wadia
 
Php tutorial
Niit
 
Introduction to PHP
Jussi Pohjolainen
 
Ad

Similar to PHP - Introduction to String Handling (16)

DOCX
Regular expressionfunction
ADARSH BHATT
 
PPT
Regular Expressions in PHP, MySQL by programmerblog.net
Programmer Blog
 
PPTX
Regular expressions in php programming language.pptx
NikhilVij6
 
PPTX
Php pattern matching
JIGAR MAKHIJA
 
PPT
Regex Basics
Jeremy Coates
 
PPTX
Regular Expressions in PHP
Andrew Kandels
 
PDF
lab4_php
tutorialsruby
 
PDF
lab4_php
tutorialsruby
 
PPTX
200 Days of Code, Beginner Track, Month 5
Ryne McCall
 
PPTX
Unit 1-strings,patterns and regular expressions
sana mateen
 
PPTX
Strings,patterns and regular expressions in perl
sana mateen
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PDF
String functions
Nikul Shah
 
Regular expressionfunction
ADARSH BHATT
 
Regular Expressions in PHP, MySQL by programmerblog.net
Programmer Blog
 
Regular expressions in php programming language.pptx
NikhilVij6
 
Php pattern matching
JIGAR MAKHIJA
 
Regex Basics
Jeremy Coates
 
Regular Expressions in PHP
Andrew Kandels
 
lab4_php
tutorialsruby
 
lab4_php
tutorialsruby
 
200 Days of Code, Beginner Track, Month 5
Ryne McCall
 
Unit 1-strings,patterns and regular expressions
sana mateen
 
Strings,patterns and regular expressions in perl
sana mateen
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
String functions
Nikul Shah
 
Ad

More from Vibrant Technologies & Computers (20)

PPT
Buisness analyst business analysis overview ppt 5
Vibrant Technologies & Computers
 
PPT
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to SQL database
Vibrant Technologies & Computers
 
PPT
ITIL - introduction to ITIL
Vibrant Technologies & Computers
 
PPT
Salesforce - Introduction to Security & Access
Vibrant Technologies & Computers
 
PPT
Data ware housing- Introduction to olap .
Vibrant Technologies & Computers
 
PPT
Data ware housing - Introduction to data ware housing process.
Vibrant Technologies & Computers
 
PPT
Data ware housing- Introduction to data ware housing
Vibrant Technologies & Computers
 
PPT
Salesforce - classification of cloud computing
Vibrant Technologies & Computers
 
PPT
Salesforce - cloud computing fundamental
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to advanced sql concepts
Vibrant Technologies & Computers
 
PPT
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
PPT
SQL- Introduction to SQL Set Operations
Vibrant Technologies & Computers
 
PPT
Sas - Introduction to designing the data mart
Vibrant Technologies & Computers
 
PPT
Sas - Introduction to working under change management
Vibrant Technologies & Computers
 
PPT
SAS - overview of SAS
Vibrant Technologies & Computers
 
PPT
Teradata - Architecture of Teradata
Vibrant Technologies & Computers
 
PPT
Teradata - Restoring Data
Vibrant Technologies & Computers
 
Buisness analyst business analysis overview ppt 5
Vibrant Technologies & Computers
 
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 
SQL- Introduction to SQL database
Vibrant Technologies & Computers
 
ITIL - introduction to ITIL
Vibrant Technologies & Computers
 
Salesforce - Introduction to Security & Access
Vibrant Technologies & Computers
 
Data ware housing- Introduction to olap .
Vibrant Technologies & Computers
 
Data ware housing - Introduction to data ware housing process.
Vibrant Technologies & Computers
 
Data ware housing- Introduction to data ware housing
Vibrant Technologies & Computers
 
Salesforce - classification of cloud computing
Vibrant Technologies & Computers
 
Salesforce - cloud computing fundamental
Vibrant Technologies & Computers
 
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
SQL- Introduction to advanced sql concepts
Vibrant Technologies & Computers
 
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
SQL- Introduction to SQL Set Operations
Vibrant Technologies & Computers
 
Sas - Introduction to designing the data mart
Vibrant Technologies & Computers
 
Sas - Introduction to working under change management
Vibrant Technologies & Computers
 
SAS - overview of SAS
Vibrant Technologies & Computers
 
Teradata - Architecture of Teradata
Vibrant Technologies & Computers
 
Teradata - Restoring Data
Vibrant Technologies & Computers
 

Recently uploaded (20)

PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 

PHP - Introduction to String Handling

  • 3. StringStringlerler $a = trim($name); //kırpma $a = nl2br(“line1nline2”); // “line1 br line2”< > çevirir printf(“total %d”, 15); // prints to stdout
  • 4. Strings in PHPStrings in PHP A string is an array of character. $a = strtoupper($name); // büyük harf $a = strtolower($name); // küçük harf $a = ucfirst($name); // İlk karakterbüyük $text = "A very long woooooooooooord."; $newtext = wordwrap($text, 8, "n", 1); $a = crpyt($a); // şifreleme $a = decrypt(encrpyt($a)); // 2-way encription with Mcrpt extension
  • 5. Strings in PHPStrings in PHP Slash ekler- (veritabanına eklerken) $a = AddSlashes($typedText); Slashları kaldırır $a = StripSlashes($typedText);
  • 6. StringString birleştirme vebirleştirme ve ayırmaayırma ?< $pizza = "piece1 piece2 piece3 piece4 piece5"; $pieces = explode (" ", $pizza); // split string into pieces for($i=0; $i count($pieces); $i++)< echo "- $pieces[$i] br ";> < > echo implode(“:", $pieces); // join strings using “:“ ?>
  • 7. Stringleri ayırmaStringleri ayırma $string = "This is an example string"; $tok = strtok ($string," "); while ($tok) { echo "Word=$tok<br>"; $tok = strtok (" "); }
  • 8. Strings in PHPStrings in PHP • string substr (string string, int start [, int length]) • int strlen (string str) • int strcmp (string str1, string str2) Returns o < 0 if str1 is less than str2; o > 0 if str1 is greater than str2, o 0 if they are equal.
  • 9. Regular ExpressionsRegular Expressions • A way of describing a pattern in string • Use special characters to indicate meta-meaning in addition to exact matching. • More powerful than exact matching • There are 2 sets of function on regular expressions in PHP o Functions using POSIX-type reg expr o Functions using Perl-type reg expr
  • 10. Regular ExpressionsRegular Expressions “.” tek bir karakterle eşleşir .at == “cat”, “sat”, etc. [a-zA-Z0-9] tek bir karakterle (a-zA-Z0-9) arasında eşleşir. [^0-9] rakam olmayan birşeyle eşleşir.
  • 11. Regular Expr: Built-inRegular Expr: Built-in Char-setsChar-sets • [[:alphanum:]] --harf • [[:digit:]] rakamla • [[:space:]] boşlukla
  • 12. Regular ExprRegular Expr • . Tek karakter • + 1 ya da daha fazla bulunan stringle • * 0 ya da daha fazla bulunan stringle • [a-z] karakter • ^ değil anlamında • $ string sonu • | or • özel karakterleri atlar • (sub-expr) -- sub-expression • (sub-expr){i,j} i min i, max j ile sub-expr olma durumu
  • 13. Reg Expr Functions (Perl)Reg Expr Functions (Perl) • preg_match — Perform a reg expr match • preg_match_all — Perform a global r.e. match • preg_replace — Perform a re search & replace • preg_split — Split string by a reg expr
  • 14. preg_match -- Perform apreg_match -- Perform a re matchre match int preg_match (string pattern, string subject [, array matches]) • Searches subject for a match to the reg expr given in pattern. • Return one match for each subpattern () only • $matches[0]: the text matching the full pattern • $matches[1]: the text that matched the first captured parenthesized subpattern, and so on. • Returns true if a match for pattern was found
  • 15. preg_match -- Perform apreg_match -- Perform a re matchre match preg_match("/pattern/modifier", subject, array) Modifiers: • i: case insensitive search • m: by default subject is treated single-line even if it contains newlines, m makes PCRE treat subject multiline (for ^, $) • s: makes . metacharacter match n • x: whitespace in pattern is ignored • E: $ matches only at the end of subject • U: behave ungreedy (comert)
  • 16. preg_match -- Perform apreg_match -- Perform a re matchre match$s = <<<STR <table><tr><td>cell1</td><td>cell2</td></tr> <tr><td>cell3</td><td>cell4</td></tr></table> STR; preg_match("/<table>(.*)</table>/Us", $s, $r) // anything between <table> and </table> preg_match("/<tr><td>(.*)</td><td>(.*)</td></tr>/Us", $r[1], $t) // matches cell1 and cell2 preg_match("/<tr>(.*)</tr>/Us", $r[1], $t); // matches <td>cell1</td><td>cell2</td>
  • 17. preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch int preg_match_all (string pattern, string subject, array matches [, int order]) • Searches subject for all matches and puts them in matches in the order specified by order. • After the first match, the subsequent ones are continued on from end of the last match. • $matches[0] is an array of full pattern matches • $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
  • 18. preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch preg_match("/<table>(.*)</table>/Us", $s, $r); preg_match_all("/<tr><td>(.*)</td><td>(.*)</td></ tr>/Us", $r[1], $t); echo $t[1][0],$t[1][1],$t[2][0],$t[2][1]; // prints cell1cell3cell2cell4 preg_match_all("/<tr><td>(.*)</td><td>(.*)</td></ tr>/Us", $r[1], $t, PREG_SET_ORDER ); //Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches,… echo $t[0][1],$t[0][2],$t[1][1],$t[1][2]; // returns cell1cell2cell3cell4
  • 19. preg_matchpreg_match_all:_all: Perform Perform globalglobal matchmatch Back reference preg_match_all ("/(<([w]+)[^>]*>)(.*)(</2>)/", $html, $matches); // 2 means [w]+ preg_match_all ("|<[^>]+>(.*)</[^>]+>|U", $html, $m) // return text in html tags
  • 20. Convert HTML to TextConvert HTML to Text $html = file(“http://www.page.com”); $search = array ("'<script[^>]*?>.*?</script>'si", "'<[/!]*?[^<>]*?>'si", "'([rn])[s]+'", "'&(quote|#34);'i", "'&(amp|#38);'i", …); $replace = array ("", "", "1", """, "&", …); $text = preg_replace ($search, $replace, $html);
  • 21. Reg Expr FunctionsReg Expr Functions (POSIX)(POSIX) • ereg (string pattern, string string [, array regs]) o Searches a string for matches to the regular expression given in pattern. • ereg_replace (string pattern, string subs, string string) o Scans string for matches to pattern, then replaces the matched text with subs. • array split (string pattern, string string [, int limit]) o Split string using pattern
  • 22. Regular ExprRegular Expr ereg ("abc", $string); ereg ("^abc", $string); ereg ("abc$", $string); eregi ("(ozilla.[23]|MSIE.3)", $HTTP_USER_AGENT); ereg ("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)", $string,$regs); $string = ereg_replace ("^", "<BR>", $string); $string = ereg_replace ("$", "<BR>", $string); $string = ereg_replace ("n", "", $string);
  • 23. Regular ExprRegular Expr if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) { echo "$regs[3].$regs[2].$regs[1]"; }else{ echo "Invalid date format: $date"; }
  • 24. Regular ExprRegular Expr $string = "This is a test"; echo ereg_replace (" is", " was", $string); echo ereg_replace ("( )is", "1was", $string); echo ereg_replace ("(( )is)", "2was", $string);
  • 25. Regular ExprRegular Expr $date = "04/30/1973"; // Delimiters may be slash, dot, or hyphen list ($month, $day, $year) = split ('[/.-]', $date); echo "Month: $month; Day: $day; Year: $year";
  • 26. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/php-classes-in- mumbai.html