Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

New programmers must know what PHP regular expressions are.

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the new programmers must know what PHP regular expressions are, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Regular expressions are a very useful feature for developers to find, match, and replace sentences, words, or strings in other formats. The following editor will explain what PHP regular expressions new programmers must know?

New programmers must know what PHP regular expressions are.

Verify domain name verify whether a string is a valid domain name

$url= "http://komunitasweb.com/";

If (preg_match ('/ ^ (http | https | ftp): / / ([A-Z0-9] [A-Z0-9 steps -] *) (. [A-Z0-9] [A-Z0-9 steps -] *) +): (d +) / / iFengjingjue url) {

Echo "Yoururlisok."

} else {

Echo "Wrongurl."

}

Highlight a word from a string

This is a very useful search result that matches a word in a string and highlights it.

$text= "SamplesentencefromKomunitasWeb,regexhasbecomepopularinwebprogramming.Nowwelearnregex.Accordingtowikipedia,Regularexpressions (abbreviatedasregexor

Regexp,withpluralformsregexes,regexps,orregexen) arewritteninaformallanguagethatcanbeinterpretedbyaregularexpressionprocessor "

$text=preg_replace ("/ b (regex) bzag I", '1century dagger text)

Echo$text

Highlight the query results in your WordPress blog, as I just said, the above code can easily search for results, and here is a better way to perform a search to open your file search.php on a WordPress blog, then find the method the_title () and replace it with the following code

Echo$title

Now,justbeforethemodifiedline,addthiscode:

$title=get_the_title ()

$keys=explode ("", $s)

$title=preg_replace ('/ ('.implode (' |, $keys).') / iu'

'\ 0'

$title)

>

Savethesearch.phpfileandopenstyle.css.Appendthefollowinglinetoit:

Strong.search-excerpt {background:yellow;}

Get all the pictures from the HTML document

If you ever wanted to get all the pictures on a web page, this code is what you need, and you can easily build a picture download robot.

$images=array ()

Preg_match_all ('/ (img | src) = ("|') [^"'>] + / iFengjinghuajiajingmedia)

Unset ($data)

$data=preg_replace ('/ (img | src) ("|'| =") (. *) / iTunes, "$3", $media [0])

Foreach ($dataas$url)

{

$info=pathinfo ($url)

If (isset ($info ['extension']))

{

If (($info ['extension'] = =' jpg') | |

($info ['extension'] = =' jpeg') | |

($info ['extension'] = =' gif') | |

($info ['extension'] = =' png'))

Array_push ($images,$url)

}

}

Delete duplicate letters

The expression of repeatedly entering letters is appropriate.

$text=preg_replace ("/ s (winters) 1 text I", "$1")

Delete duplicate punctuation

The function is the same as above, but it is only a comma repeated in the face of punctuation.

$text=preg_replace ("/. + / I", ".", $text)

Match a XML or HTML tag

This simple function takes two arguments: the first is the tag you want to match, and the second is the variable that contains XML or HTML. Again, this is really powerful.

Functionget_tag ($tag,$xml) {

$tag=preg_quote ($tag)

Preg_match_all ('{] * > (. *).}'

$xml

$matches

PREG_PATTERN_ORDER)

Return$matches [1]

}

Match XML or HTML tags with attribute values

This function is very similar to the above, but it allows you to match tags with attribute values inside, for example, you can easily match

Functionget_tag ($attr,$value,$xml,$tag=null) {

If (is_null ($tag))

$tag='\ wicked'

Else

$tag=preg_quote ($tag)

$attr=preg_quote ($attr)

$value=preg_quote ($value)

$tag_regex= "/] * $attr\ swords =\ s *".

"(['\"]) $value\\ 2 [^ >] * > (. *) / "

Preg_match_all ($tag_regex

$xml

$matches

PREG_PATTERN_ORDER)

Return$matches [3]

}

Match hexadecimal color values

Another interesting tool for web developers that allows you to match and verify hexadecimal color values.

$string= "# 555555"

If (preg_match ('/ ^ # (: (: [a-fd] {3}) {1Magazine 2}) $/ iMagazine string)) {

Echo "example6successful."

}

Find the page title

This code makes it easy to find and print content between web pages.

$fp=fopen ("http://www.catswhocode.com/blog","r");

While (! feof ($fp)) {

$page.=fgets ($fp,4096)

}

$titre=eregi ("", $page,$regs)

Echo$regs [1]

Fclose ($fp)

New programmers must know what PHP regular expressions are.

Interpret the Apache log

Most websites use famous Apache servers, so if your site is the same, how about using PHP regular expressions to parse apache server logs?

/ / Logs:Apachewebserver

/ / SuccessfulhitstoHTMLfilesonly.Usefulforcountingthenumberofpageviews.

'^ ((# clientIPordomainname) Songs +) ((# basicauthentication) SignorsSecretsPlus) [((# dateandtime) [^]] +)] s + "(: GET | POST | HEAD) ((# file) / [^"] + .html) ((# parameters) [^ "] +) HTTP/ [0-9.] +" s + (# statuscode) 200s+ ((# bytestransferred) [- 0-9] +) s+ "(# referrer) [^"] *) "s+" ((# useragent) [^ "] *)" $'

/ / Logs:Apachewebserver

/ / 404errorsonly

'^ (# clientIPordomainname) S +) s + [(# dateandtime) [^]] +] s + "(: GET | POST | HEAD) ((# file) [^"] +) ((# parameters) [^ "] +) HTTP/ [0-9.] +" s + (# statuscode) 404s + ((# bytestransferred) [- 0-9] +) s + ((# referrer) [^ "] *)" s + ((# useragent) [^ "] *)" $'

Use smart quotes instead of double quotes

If you are a printing enthusiast, you will like this regular expression that allows smart quotes instead of double quotes, which is used by WORDPRESS on its content

Preg_replace ('B "b ([^" x84x93x94rn] +) b "Bai Zhi Ji 1 Zhi text)

Verify the complexity of the password

This regular expression will detect whether the input contains 6 or more letters, numbers, underscores and hyphens. Input must contain at least one uppercase letter, one lowercase letter and one number

'a (= [- _ a-zA-Z0-9] * [Amurz]) (= [- _ a-zA-Z0-9] * [Amurz]) (= [- _ a-zA-Z0-9] * [0-9]) [- _ a-zA-Z0-9] {6,} z'

WordPress: use regular to get pictures on a post

I know that many people are WORDPRESS users, and you may like and be willing to use the image code that is retrieved from the content of the post. To use this code in your BLOG, you just need to copy the following code to one of your files

$szPostContent=$post- > post_content

$szSearchPattern='~] * / > ~'

/ / Runpreg_match_alltograballtheimagesandsavetheresultsin$aPics

Preg_match_all ($szSearchPattern,$szPostContent,$aPics)

/ / Checktoseeifwehaveatleast1image

$iNumberOfPics=count ($aPics [0])

If ($iNumberOfPics > 0) {

/ / Nowhereyouwoulddowhateveryouneedtodowiththeimages

/ / Forthisexampletheimagesarejustdisplayed

For ($iSuppli

Automatically generate smiley face pattern

Another method used by WordPress, this code allows you to automatically change the image to a smiley face symbol.

$texte='Atextwithasmiley'

Echostr_replace (': -)',', $texte)

Remove the link to the picture

$str='

Jobbole other characters

/ / echopreg_replace ("/ () /",'$2 million

Echopreg_replace ("/ () /",'\ 2)

>

About the new programmer must know what PHP regular expressions have to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 220

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report