Regular expressions php character replacement. PHP regular expressions. Excluded Character Classes

PHP, as befits a modern programming language, offers the developer a set of functions for using regular expressions. You can search for occurrences of strings in other strings using complex criteria.

  • PREG_PATTERN_ORDER;
  • PREG_SET_ORDER.

Sort by Option 1 groups search results by regular expression number (the default). In the second case, the results are grouped by their location in the row.

Symbol - template element

It is important to remember that the template operates on symbols. Programming has long forgotten what the "character" data type is. Modern languages ​​do not go below the concept of a "string", but with regard to the pattern, you need to understand: symbols are manipulated here.

Building a template is, first of all, specifying the required sequence of characters. If you clearly understand this, then there will be no errors in the template. In any case, it will be much less.

  • a is a specific element of the template - a symbol.
  • a-z is a template element, also one character, but only with the value from a to z - all Latin in lowercase.
  • 0-9 is one number, any number, but 1-3 is only 1, 2 or 3.

Case in the pattern is important. The first and last characters of the pattern are significant. You can specify where the template begins and ends.

Function template

PHP preg match all uses standard regular expression syntax. indicate one of the symbols indicated in them:

  • only characters a, b, c.
  • [^ABC] everything except the characters A, B, C.
  • \w and \W - text or non-text character.
  • \s and \S - whitespace or non-whitespace character.
  • \d and \D - digit or non-digit.

Repeat characters are indicated by curly braces - (n,m) and refer to the previous character.

  • n denotes repetition of "not less than";
  • m - repetition of "no more".

The syntax provides many options for creating templates, but it is best to start with the basics, that is, with simple ones written in your own hand, in which complex elements and combinations are absent.

Simply put, by listing the real characters that are needed, indicating their required quantities, and taking into account that the “^” character corresponds to the beginning, and “$” to the end of the line, you can create simple templates. By analyzing real, well-debugged regular expressions from qualified experts, you can gain solid knowledge to create complex applications of preg match all. PHP's arsenal is not limited to just these two functions, but they are the most commonly used.

Simple Practice

Integer pattern:

  • "/*/"

Also an integer pattern, but there may be a sign in front ("+", "-"), and there may be extra spaces at the front/back:

  • /^[\s|\+|\-](0,1)*/

Likewise:

  • /^[\s|\+|\-](0,1)*(\.)*/ - a number with a dot.
  • /+@+\.(2,3)/ - option for e-mail recognition.

Using your own templates for preg match all, examples of them on the Internet, analyzing the code of website pages and other sources allows you to create your own library of templates.

There are many options for finding information. In particular, the last two constructions given above can be modeled differently. In many cases, preference will be given to the template that provides the desired match faster and more accurately. Using preg match all in PHP, as well as similar functions in other languages, requires practice, attention, and preliminary checking of the correctness of the templates.

This article provides a selection of php regexp examples. A very nice and useful collection of regular expression examples. All regular expression examples are acceptable for PHP. Enjoy it for your health!

Domain name verification example

This php snippet checks whether the string is a valid domain name.

?:.*)+):?(d+)?/?/i", $url)) ( echo "Your url is ok."; ) else ( echo "Wrong url."; )

Example of highlighting a word in text

A very useful regular expression for searching and highlighting the desired word in the text. The code is especially useful when creating search results output.

$text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor"; $text = preg_replace("/b(regex)b/i", " 1", $text); echo $text;

An example of the implementation of highlighting search resultsWordPress

Open the search.php file and find the_title() function. Replace it with the following line:

Echo $title;

Now, before the replaced line, insert this code:

\0", $title); ?>

Save your search.php file and open style.css. Add the following line to it:

Strong.search-excerpt ( background: yellow; )

An example of obtaining images fromHTML regexp method

This piece of PHP code using regular expressions searches for all images and their url.

$images = array(); preg_match_all("/(img|src)=("|")[^"">]+/i", $data, $media); unset($data); $data=preg_replace("/(img|src)("|"|="|=")(.*)/i","$3",$media); foreach($data as $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); ) )

Removing duplicate words (case insensitive)

Are there often words that are repeated? Then this regular expression example will be useful to you.

$text = preg_replace("/s(w+s)1/i", "$1", $text);

Removing duplicate points

The same thing, only with repeating points.

$text = preg_replace("/.+/i", ".", $text);

XML/HTML tag matching

This simple function takes two arguments: a tag (which you want to match), xml, or html code.

Function get_tag($tag, $xml) ( $tag = preg_quote($tag); preg_match_all("(<".$tag."[^>]*>(.*?).")", $xml, $matches, PREG_PATTERN_ORDER); return $matches; )

Search for XHTML/XML tags with specific attribute values

This example is similar to the previous function, only you can significantly expand the search, for example find

.

Function get_tag($attr, $value, $xml, $tag=null) ( if(is_null($tag)) $tag = "\w+"; else $tag = preg_quote($tag); $attr = preg_quote($ attr); $value = preg_quote($value); $tag_regex = "/<(".$tag.")[^>]*$attr\s*=\s*". "(["\"])$value\\2[^>]*>(.*?)<\/\\1>/" preg_match_all($tag_regex, $xml, $matches, PREG_PATTERN_ORDER); return $matches; )

Finding hexadecimal color values

A great example of a regular expression that matches hexadecimal color values ​​in given strings. What is this for? Maybe you want to write a CSS code compression service, or something similar.

$string = "#555555"; if (preg_match("/^#(?:(?:(3))(1,2))$/i", $string)) ( echo "example 6 successful."; )

Search exampletitle on a given page

This interesting example of PHP code with regexp finds and returns text between tags And.

Feof($fp))( $page .= fgets($fp, 4096); ) $titre = eregi(" (.*)",$page,$regs); echo $regs; fclose($fp);

Apache log parsing

Most sites run on well-known Apache servers. If your site also runs on it, then you can parse the server log using php regexp.

//Logs: Apache web server //Successful hits to HTML files only. Useful for counting the number of page views. "^((?#client IP or domain name)S+)s+((?#basic authentication)S+s+S+)s+[((?#date and time)[^]]+)]s+"(?: GET|POST|HEAD) ((?#file)/[^ ?]+?.html?)??((?#parameters)[^ ?]+)? HTTP/+"s+(?#status code)200s+((?#bytes transferred)[-0-9]+)s+"((?#referrer)[^"]*)"s+"((?#user agent )[^"]*)"$" //Logs: Apache web server //404 errors only "^((?#client IP or domain name)S+)s+((?#basic authentication)S+s+S+) s+[((?#date and time)[^]]+)]s+"(?:GET|POST|HEAD) ((?#file)[^ ?"]+)??((?#parameters)[ ^ ?"]+)? HTTP/+"s+(?#status code)404s+((?#bytes transferred)[-0-9]+)s+"((?#referrer)[^"]*)"s+"((?#user agent )[^"]*)"$"

Example of checking password complexity

An excellent example of a regular expression that checks the complexity level of a password. The password must be 6 characters long and contain at least one uppercase character, a lowercase character, and a number.

"A(?=[-_a-zA-Z0-9]*?)(?=[-_a-zA-Z0-9]*?)(?=[-_a-zA-Z0-9]*?) [-_a-zA-Z0-9](6,)z"

Replacing text emoticons with graphic emoticons

This example code will change the text emoticon to your graphic one. Interesting and useful PHP snippet.

$texte="A text with a smiley:-)"; echo str_replace(":-)"," ",$texte);

Example of a regular expression to get images fromhtml code

It is worth saying that this php code is used in wordpress to search and process images.

post_content; $szSearchPattern = "~ ]* />~"; // Run preg_match_all to grab all the images and save the results in $aPics preg_match_all($szSearchPattern, $szPostContent, $aPics); // Check to see if we have at least 1 image $iNumberOfPics = count($aPics); if ($iNumberOfPics > 0) ( // Here you can process your images // In this example they will simply be displayed on the monitor for ($i=0; $i< $iNumberOfPics ; $i++) { echo $aPics[$i]; }; }; endwhile; endif; ?>

I hope you found this collection of php regexp examples useful. If there are interesting additions or examples of regular expressions (php), write in the comments.

Function description:

The preg_replace function searches for matches of a regular expression ( pattern ) with data in a string ( subject ) and replaces them with the data found in replacement .

Argument

Description

pattern

Required argument. Regular expression (search pattern)

string, array

replacement

Required argument. A string or array containing data used to replace data in subject

You can also use a construction of the form \$(1) for links, which is equivalent to \$1; this construction is used if the link is immediately followed by a number (\$(1)1).

string, array
subject Required argument. A string or array containing the data that will be replaced. string, array
limit

Optional argument. Maximum number of substitutions. Default is -1 (unlimited)

int
count

Optional argument. A variable that contains the number of replacements made.

int

The preg_replace() function can be used with the /e modifier. When this modifier is present, the replacement parameter is interpreted as PHP code and then used for replacement.

Regular Expressions

Separately, we should dwell on regular expressions, without an understanding of which it is quite difficult (if not impossible) to understand the work of the function preg_replace().

A fairly good description of regular expressions is given on wikipedia.org, the main points of using regular expressions are also described

Examples:

Example 1:

In this example, we replace all numbers placed in brackets with asterisks.

Example 2:

In this example, we replace the string corresponding to the entire pattern with the data corresponding to the first subpattern at reference \$1. In other words, "have 3 apples" corresponding to "/(\w+) (\d+) (\w+)/" will be replaced by "have" corresponding to (\w+).

Example 3:

This example shows what happens if you use arrays as pattern and replacement.

Example 4:

This example shows what happens if you use arrays as pattern, replacement and subject.

Example 5:

This example shows the use of the /e modifier, i.e. when replacement is interpreted as php code. Below is the conversion of characters from lower to upper case.

Example 6:

This example shows the use of the count argument. Here the result will be equal to 2, so there were two replacements.

mixed preg_match(string pattern, string subject [, array &matches [, int flags [, int offset]]])

Searches the given text subject for matches with the pattern pattern

If the additional matches parameter is specified, it will be filled with search results. The $matches element will contain the portion of the string that matches the entire pattern, $matches will contain the portion of the string that matches the first subpattern, and so on.

flags can take the following values:

PREG_OFFSET_CAPTURE

If this flag is specified, for each found substring its position in the source string will be indicated. It is important to remember that this flag changes the format of the returned data: each occurrence is returned as an array, the zeroth element of which contains the found substring, and the first element contains the offset. This flag is available in PHP 4.3.0 and higher.

The additional flags parameter is available since PHP 4.3.0.

The search is carried out from left to right, from the beginning of the line. The optional offset parameter can be used to specify an alternative starting position for the search. The additional offset parameter is available since PHP 4.3.3.

Comment: Using the offset parameter is not equivalent to replacing the matched string with substr($subject, $offset) when calling the function preg_match_all(), since pattern may contain conditions such as ^ , $ or (? . Compare:

While this example

Function preg_match() returns the number of matches found. It can be 0 (no matches found) and 1 because preg_match() stops working after the first match found. If you need to find or count all matches, you should use the function preg_match_all(). Function preg_match() returns FALSE in case any errors occur during execution.

Clue: Don't use the function preg_match(), if you need to check for the presence of a substring in a given string. Use for this strpos() or strstr(), since they will complete this task much faster.


Example 2. Search for the word "web" in the text

/*
The special sequence \b in the pattern means a word boundary,
therefore, only an isolated occurrence of the word "web" will match
mask, as opposed to "webbing" or "cobweb".
*/
if (preg_match("/\bweb\b/i" , "PHP is the web scripting language of choice.")) {
echo "Entry found.";
) else (
echo "No entry found.";
)preg_match ("/\bweb\b/i" , "PHP is the website scripting language of choice.")) {
echo "Entry found.";
) else (
echo "No entry found.";
}
?>

I've been wanting to understand regular expressions for a long time. Although “figure it out” is a strong word. To master the great art of becoming a master of regular expressions, you need to constantly work with them. It’s not enough to learn the syntax, special characters and modifiers - you need to be able to use them. And the ability to use comes with experience.

In this post I will post examples of using regular expressions, the work of which I myself have understood.

Table of special characters

Special character Description
\ Escape symbol. Example: ‘/seo\/smo/’- matches the line that contains seo/smo.
^ Start of data symbol. Example: ‘/^seo/’— matches a line that begins with the word seo.
$ End of data symbol. Example: ‘/blog$/’— matches a line that ends with the word blog.
. Any character other than line feed. Example: ‘/seo.ult/’- matches the string seopult, seo9ult, seo@ult, etc.
Inside these brackets are listed characters, any of which can appear at a given place, but only one. Example: ‘/seoult/’— only lines containing seopult, seokult or seomult will be matched.
| Or. Example below.
() Submask.
? One or zero occurrences of the preceding character or subpattern.
* Any number of occurrences of the preceding character or subpattern. Including zero.
+ One or more occurrences.
Example: ‘/se+(op|om)?.*t/’- the letter s, then one or more letters e, after which the combination op or om can appear once, or maybe not once, then any number of any symbols and the letter t.
(a,b) The number of occurrences of the preceding character or subpattern from a to b. Example: (0,) - the same as *, (0,1) - the same as ?, (3,5) - 3, 4 or 5 repetitions.

Simple examples of PHP scripts using regular expressions:

1) Previous and subsequent expressions.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // find a word in which ing comes after Box. If the word is found, the function will return true, if not, false.$pattern1 = "/Box(?=ing)/" ; preg_match($pattern1, "Box Day"); // false preg_match ($pattern1, "Boxing Day" ) ; // true // find a word that does not have ing after box. If the word is found, the function will return true, if not, false.$pattern2 = "/box(?!ing)/" ; preg_match($pattern2, "Box for iPhone and iPad"); // true preg_match ($pattern2, "What is boxing day?") ; // false preg_match ($pattern2, "css-moz-box-shadow" ) ; // true // find a word where ing is not preceded by box. If the word is found, the function will return true, if not, false.$pattern3 = "/(?

[^<]+?~","seo blog",$text); echo $text; ?>

3) Get and display the Alexa Rank value of a given site.

1 2 3 4 5 6 7 8 9 "#
(.*?)
#si", file_get_contents ( "http://www.alexa.com/siteinfo/($url)") , $a ) ; return trim (str_replace ("," , "" , strip_tags ($a [ 1 ] ) ) ) ; ) $alexa = alexa($url) ; echo $alexa ; ?>

(.*?)

#si", file_get_contents("http://www.alexa.com/siteinfo/($url)"), $a); return trim(str_replace(",","",strip_tags($a))); ) $alexa = alexa($url); echo $alexa; ?>

4) Get and display the title of the page.

1 2 3 4 5 (.*)<\/title>/s" , $str , $m ) ; echo $m [ 1 ] ; ?>

(.*)<\/title>/s", $str, $m); echo $m; ?>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 \n \r \t ]/", "" , $content ) ; // replace newline and tab characters with spaces$content = preg_replace ("/(2,)/" , " " , $content ) ; // replace more than 2 spaces with one preg_match_all("/ ]*href=(?:"| \" )(.*)(?:"|\" )[^<>]*>(.*)<\/a>/iU", $content , $links ) ; // collect links if (sizeof ($links [ 1 ] ) > 0 ) // if links are found( $out = array () ; // array of external links foreach ($links [ 1 ] as $v ) ( if (preg_match ("/http:\/\/(www\.)(0,1)" . $domain . "/i" , $v ) ) // weed out internal links( continue ; ) if ( preg_match ( "/(http:|https:)?\/\/(www\.)(0,1)(.*)/i", $v ) ) // external reference( $out = $v ; ) ) return $out ; ) return array () ; ) $domain = "site" ; $content = file_get_contents ("http://site/" ) ; $getoutlinks = getoutlinks($content, $domain) ; for ($i = 0; $i<= count ($getoutlinks ) ; $i ++ ) { echo $getoutlinks [ $i ] . "
" ; } ?>

]*href=(?:"|\")(.*)(?:"|\")[^<>]*>(.*)<\/a>/iU", $content, $links); // collect links if (sizeof($links) > 0) // if links are found ( $out = array(); // array of external links foreach ($links as $v ) ( if (preg_match("/http:\/\/(www\.)(0,1)".$domain."/i", $v)) // weed out internal links ( continue; ) if (preg_match ("/(http:|https:)?\/\/(www\.)(0,1)(..com/"); $getoutlinks = getoutlinks($content, $domain); for ($i = 0;$i<= count($getoutlinks); $i++) { echo $getoutlinks[$i]."
"; } ?>

Where:
preg_replace— Performs search and replace using a regular expression.
preg_match— performs a check against a regular expression.
preg_match_all— finds all matches, while preg_match only finds the first one.
file_get_contents— get the contents of the file as one line.
trim— removes spaces at the beginning and end of a line.
str_replace— replaces the search string with a replacement string.
strip_tags— removes html and php tags from a string.
sizeof— gets the number of elements in the variable.
count— counts the number of array elements or the number of object properties.