A few examples of regular expressions
Is the string a number up to 77 digits long:
If (ereg ("^ [0-9] {1,77} $", $ string)) echo "yes"; Else echo "no";
Whether the string consists only of letters, numbers and "_", a length of 5 to 20 characters:
If (ereg ("^ [a-za-y0-9 _] {5,20} $", $ string)) echo "yes"; Else echo "no";
Is there any character in the string, other than allowed. Letters, numbers and "_" are considered valid. Length here can not be checked, except that it is simply an additional condition of strlen ($ string). Do not confuse with the previous example - even though the result is the same, but the method is different, "by contradiction"
If (! Ereg ("[^ a-za-y0-9 _]", $ string)) Echo "no foreign letters (OK)"; Else Echo "is a foreign letter (FALSE)";
For a case-insensitive comparison, use ereg i ().
Are there any consecutive characters in the string, at least 3 characters in a row (such as "abdGDDeE", but not "AABBAABB"):
If (preg_match ("/ (.) \\ 1 \\ 1 /", $ string)) echo "yes"; Else echo "no";
Replace everywhere in the text LINE1 on LINK2 (the problem is solved without regegular expressions):
$ String = str_replace ("LINE1", "LINE2", $ string);
Replace the curves of the line transition codes to normal ones: for this you need only delete "\ r".
Transitions are normal (but different!): "\ N" or "\ r \ n".
Still there are glitches, type "\ r \ r \ n".
$ String = str_replace ("\ r", "", $ string);
Replace all duplicate spaces with one. Do not try to apply str_replace here, this is a good function, but not for this example.
$ String = preg_replace ("/ XX + /", "X", $ string); // put a space instead of X
There are some words in the text, let's say "WORD" and "LYALYAL" (etc.), which must be replaced in the same way with the same thing, but with additives.
Perhaps there are no words or there are many times in any register.
Those. If there was a "word" or "word" (or else like that), you need to replace it with "<b> word </ b>" or "<b> SLOvO </ b>" (see how it was).
In other words, it is necessary to find a list of words in any register and insert fixed lines ("<b>" and "</ b>") along the edges of the nested words.
$ String = preg_replace ("/ (word1 | word2 | halal | word99) / si", " \\ 1 ", $ string);
Find the text enclosed in some tag, for example <TITLE> ... </ TITLE> from the HTML file ( $ string - source text).
If (preg_match ("!! si", $ string, $ ok)) Echo "Tag found, text: $ ok [1]"; Else Echo "Tag not found";
Find the text enclosed in some tag and replace it with another tag, for example: <TITLE> ... </ TITLE> replace the same in <MY_TEG> ... </ MY_TEG> in the HTML file:
Preg_replace ("! ! Si "," <MY_TEG> \\ 1 МОЙ_ТЕГ> ", $ String);
Highlighting PHP code in messages
For example, you have a forum like vBulletin, where you can highlight the code, if you highlight it specially: [PHP] any code [/ PHP] .
In the end, after this (when viewing the message), you get a nice and colorful php-code.
And so, if you want all the pieces between [PHP] .. [/ PHP] and <? ..?> To be perceived as code and colored, then this can be done quite easily.
Text of the program.
<? // Original message: // ------------------------------------------------ ------ $ Str = ' Pamagite, does not work! Here's an example: [Php] // comment # Comment Phpinfo (); [/ Php] La la la la la la [Php] For ($ i = 0; $ i <100; $ i ++) { Ping ("- f", "www.ru"); } [/ Php] <? Echo "<a href=http://shram.kiev.ua/> click here! </a>"; Phpinfo (); ?> '; // ------------------------------------------------ ------ // suppress warnings (in highlight_string there are glitches) Error_reporting (0); // function of highlighting one piece of text Function _my _ ($ s, $ a1, $ a2) { If ($ a1! = "<?") {$ A1 = "<?"; $ A2 = "?>"; } $ S = str_replace ("\\\" "," \ "", $ s); Ob_start (); Highlight_string ($ a1. $ S. $ A2); $ S = ob_get_contents (); Ob_end_clean (); Return $ s; } // search in the text for all the pieces between <? ... or [PHP] ... $ Str = preg_replace ("! (\ [Php \] | <\?) (. *?) (\ [/ Php \] | \?>)! Ise", "_ my _ ('\\ 2', '\ \ 1 ',' \\ 3 ') ", $ str); Echo $ str; ?>
After such a program on the screen it turns out:
Pamagite, does not work! Here's an example: <?
// comment # Comment Phpinfo (); ?> For a lyalal <? For ( $ i = 0 ; $ i < 100 ; $ i ++) { Ping ( "-f" , "www.ru" ); } ?> <? Echo "<a href=http://shram.kiev.ua/> click here! </a>" ; Phpinfo (); ?> |
As you can see, everything that was between the special lines was highlighted, and the extraneous text did not change in any way. If you are going to apply for the forum, then think about the transition to new lines.
If you have the whole message - it's solid code, then use highlight_string directly, without searching for the ? ...?> In the code ...
Checking URL for correctness
This function is taken from the source of the chat.
Supports everything that only can be in the URL ...
Remember that you should not only check, but also take a new value
From the function, t. That appends "http: //" in case of its absence.
// add. Function for the removal of dangerous syvolov Function pregtrim ($ str) { Return preg_replace ("/ [^ \ x20- \ xFF] /", "", @ strval ($ str)); } // // check the URL and return: // * +1 if the URL is empty // if (checkurl ($ url) == 1) echo "empty" // * -1 if the URL is not empty, but with errors // if (checkurl ($ url) == - 1) echo "error" // * string (new URL), if the URL is found and sparse // if (checkurl ($ url) == 0) echo "all ok" // or if (strlen (checkurl ($ url))> 1) echo "all ok" // // If the protocol was not in the URL, it will be added ("http: //") // Function checkurl ($ url) { // cut left characters and trailing whitespace $ Url = trim (pregtrim ($ url)); // if empty - exit If (strlen ($ url) == 0) return 1; // check the URL for correctness If (! Preg_match ("~ ^ (??: Https? | Ftp | telnet): // (?: [A-z0-9_-] {1.32} ". "(? :: [a-z0-9 _-] {1.32})? @)?)? (?
?: [A-z0-9 -] {1,128} \.) + (?: com | net | ". "Org | mil | edu | arpa | gov | biz | info | aero | inc | name | [az] {2}) | (?! 0) (?
? ". "! 0 [^.] | 255) [0-9] {1,3} \.) {3} (?! 0 | 255) [0-9] {1,3}) (?: / [A -z0-9., _ @% & ". "? + = \ ~ / -] *)? (?: # [^ '\" & <>] *)? $ ~ I ", $ url, $ ok)) Return -1; // if not correct - exit // if there is no protokala - add If (! Strstr ($ url, ": //")) $ url = "http: //". $ Url; // replace the protocol with lower case: hTtP -> http $ Url = preg_replace ("~ ^ [az] + ~ ie", "strtolower ('\\ 0')", $ url); Return $ url; }
So for verification you need to use something like this:
$ Url = checkurl ($ url); // re-written the URL to itself If ($ url) exit ("Invalid URL");
Validation of E-mail
Checking the correctness of E-mail - check the same as the previous example.
// // checks the soap and returns // * +1, if the soap is empty // * -1, if not empty, but with an error // * string, if soap is right // Function checkmail ($ mail) { // cut left characters and trailing whitespace $ Mail = trim (pregtrim ($ mail)); // function pregtrim () take the above in the example // if empty - exit If (strlen ($ mail) == 0) return 1; If (! Preg_match ("/ ^ [a-z0-9 _-] {1,20} @ (([a-z0-9 -] + \.) + (Com | net | org | mil |". "Edu | gov | arpa | info | biz | inc | name | [az] {2}) | [0-9] {1,3} \. [0-9] {1,3} \. [0- ". "9] {1,3} \. [0-9] {1,3}) $ / is", $ mail)) Return -1; Return $ mail; }
Cutting out URLs from text and HTML curves
Sometimes you need to cut out links from the HTML text to the URL or Email.
If you do not have any curved code in HTML, then this is a very simple task for a regular type expression:
] + Href = ([^>] +) [^>] *> (. *?)
But links are different ... How to make your program, it's up to you.
You can take only 100% of the correct links, but then some curves will not be included (although they are also true).
You can take everything in a row, but then some links will not be quite correctly cut out.
Text of the program:
<? $ Str = " <a href=url1> name1 </a> <a href=url2> name2 </a> <a href='url3'> name3 </a> <a href=url4> <brackets </a> </a> <a href=\"url5\"> <b> bold </ b> </a> <a href=url6> \ "quotes \" </a> <a target=\"<try to try to outwit the program> hahaha \ "href = url7> 77777 </a> <a href=url8 target=\"< attempt to outwit the program> hahaha \ "> 88888 </a>"; Echo "<pre> Source code:". Htmlspecialchars ($ str). "</ Pre>"; Echo "--------------- Option 1 ---------------"; Preg_match_all ("!] +) \ "? '?. *?> (. *?)! Is", $ str, $ ok); For ($ i = 0; $ i ". $ Ok [1] [$ i]." - ". $ Ok [2] [$ i]; } Echo "
--------------- Option 2 --------------- "; Preg_match_all ("! ] + Href = \ "? '? ([^ \"'>] +) \ "? '? [^>] *> (. *?)! Is", $ str, $ ok); For ($ i = 0; $ i". $ Ok [1] [$ i]." - ". $ Ok [2] [$ i]; } Echo "
--------------- Option 3 --------------- "; Preg_match_all ("! ] + Href = \ "? '? ([^ \"'>] +) \ "? '? [^>] *> ([^ <>] *?)! Is", $ str, $ ok); For ($ i = 0; $ i". $ Ok [1] [$ i]." - ". $ Ok [2] [$ i]; } ?>
Example output:
Source: <a href=url1> name1 </a> <a href=url2> name2 </a> <a href='url3'> name3 </a> <a href=url4> <brackets </a> </a> <a href="url5"> <b> bold </ b> </a> <a href=url6> "quotes" </a> <a target="<Try to outwit the program> hahaha "href = url7> 77777 </a> <a href=url8 target="<A attempt to outwit the program> hahaha> 88888 </a> ---------------Option 1--------------- --------------- Option 2 --------------- --------------- Option 3 --------------- |
Comments
Commenting on, remember that the content and tone of your message can hurt the feelings of real people, show respect and tolerance to your interlocutors even if you do not share their opinion, your behavior in the conditions of freedom of expression and anonymity provided by the Internet, changes Not only virtual, but also the real world. All comments are hidden from the index, spam is controlled.