preg-match

which is the fast process strpos()/stripos() or preg_match() in php

﹥>﹥吖頭↗ 提交于 2019-12-18 17:08:42
问题 I just want to known which one will be fast of strpos()/stripos() or preg_match() functions in php. 回答1: I found this blog that has run some testes regarding your question, the result was: strpos() is 3-16 times faster than preg_match() stripos() is 2-30 times slower than strpos() stripos() is 20-100 percent faster than preg_match() with the caseless modifier "//i" using a regular expression in preg_match() is not faster than using a long string using the utf8 modifier "//u" in preg_match()

Regular expression containing one word or another

我的未来我决定 提交于 2019-12-18 10:58:07
问题 I need to create an expression matching a whole number followed by either "seconds" or ""minutes" I tried this expression: ([0-9]+)\s+(\bseconds\b)|(\bminutes\b) It works fine for seconds, but not minutes. E.g. "5 seconds" gives 5;seconds; while "5 minutes" gives ;;minutes 回答1: You just missed an extra pair of brackets for the "OR" symbol. The following should do the trick: ([0-9]+)\s+((\bseconds\b)|(\bminutes\b)) Without those you were either matching a number followed by seconds OR just the

How the preg_match handles the delimiter when \Q..\E used?

只谈情不闲聊 提交于 2019-12-18 09:02:46
问题 I'm playing with regular expressions and I tried the \Q..\E escape sequence. First try: $regex = '/\Q http:// \E/'; var_dump(preg_match($regex, ' http:// ')); It tells me that '\' is unknown modifier, completely understandable. Second try: $regex = '/\Q http:\/\/ \E/'; var_dump(preg_match($regex, ' http:// ')); var_dump(preg_match($regex, ' http:\/\/ ')); It runs, not match the first string, but match the second one. I know that I could use other delimiter character or solve it without \Q..\E

Preg Replace - replace second occurance of a match

血红的双手。 提交于 2019-12-18 07:22:58
问题 I am relatively new to php, and hope someone can help me with a replace regex, or maybe a match replace I am not exactly sure. I want to automatically bold the (second occurance of a match) and then make the 4th appearance of a match italic and then the 7th appearance of a match underlined. This is basically for SEO purposes in content. I have done some replacements with: and were thinking this should do the trick? preg_replace( pattern, replacement, subject [, limit ]) I already know the

PHP preg_match get in between string

北战南征 提交于 2019-12-18 05:39:07
问题 I'm trying to get the string hello world . This is what I've got so far: $file = "1232#hello world#"; preg_match("#1232\#(.*)\##", $file, $match) 回答1: It is recommended to use a delimiter other than # since your string contains # , and a non-greedy (.*?) to capture the characters before # . Incidentally, # does not need to be escaped in the expression if it is not also the delimiter. $file = "1232#hello world#"; preg_match('/1232#(.*?)#/', $file, $match); var_dump($match); // Prints: array(2)

Regular Expression to Match HTML <p> tag using PHP

丶灬走出姿态 提交于 2019-12-18 04:54:33
问题 I have some content like this <p>some content, paragraph 1</p> <p>some content, paragraph 2</p> <p>some content, paragraph 3</p> I would like to return the first paragraph i.e. <p>some content, paragraph 1</p> Can anyone assist me with the regex code? '<p>(.*.)</p>' doesnt seem to work 回答1: you can do it this way: if (preg_match('%(<p[^>]*>.*?</p>)%i', $subject, $regs)) { $result = $regs[1]; } else { $result = ""; } You test your string against the regular expresion, if there are some match,

Regex - match anything except specific string

自古美人都是妖i 提交于 2019-12-18 04:04:28
问题 I need a regex (will be used in ZF2 routing, I believe it uses the preg_match of php) that matches anything except a specific string. For example: I need to match anything except "red", "green" or "blue". I currently have the regex: ^(?!red|green|blue).*$ test -> match (correct) testred -> match (correct) red -> doesn't match (correct) redtest -> doesn't match (incorrect) In the last case, the regex is not behaving like I want. It should match "redtest" because "redtest" is not ("red", "green

preg_match and (non-English) Latin characters?

爷,独闯天下 提交于 2019-12-17 19:45:40
问题 I have a XHTML form where I ask people to enter their full name. I then match that with preg_match() using this pattern: /^[\p{L}\s]+$/ On my local server running PHP 5.2.13 (PCRE 7.9 2009-04-11) this works fine. On the webhost running PHP 5.2.10 (PCRE 7.3 2007-08-28) it doesn't match when the entered string contains the Danish Latin character ø ( http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=%F8&mode=char ). Is this a bug? Is there a work around? Thank you in advance! 回答1: So, the problem

PHP preg_match - only allow alphanumeric strings and - _ characters

心不动则不痛 提交于 2019-12-17 15:27:28
问题 I need the regex to check if a string only contains numbers, letters, hyphens or underscore $string1 = "This is a string*"; $string2 = "this_is-a-string"; if(preg_match('******', $string1){ echo "String 1 not acceptable acceptable"; // String2 acceptable } 回答1: Code: if(preg_match('/[^a-z_\-0-9]/i', $string)) { echo "not valid string"; } Explanation: [] => character class definition ^ => negate the class a-z => chars from 'a' to 'z' _ => underscore - => hyphen '-' (You need to escape it) 0-9

PHP email validation [duplicate]

跟風遠走 提交于 2019-12-17 10:58:18
问题 This question already has answers here : How to validate an email address using a regular expression? (73 answers) Closed 6 years ago . For PHP what is the best email validation using preg , NOT ereg because it's deprecated/removed . I don't need to check if the website exists (it's not like maximum security). I've found many ways with ereg but they (obviously) aren't good practice. 回答1: I suggest you use the FILTER_VALIDATE_EMAIL filter: if (filter_var($email, FILTER_VALIDATE_EMAIL)) { /