preg-match

Regular Expression Help for Date Validation - dd/mm/yyyy - PHP [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-05 03:29:57
This question already has an answer here: preg_match: check birthday format (dd/mm/yyyy) 11 answers Can someone show me the error of my ways when it comes to this regular expression: if(preg_match("/^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$/", $_POST["date"]) === 0) { echo 'error'; } Basically I want this to display an error message each time - unless the format is correct (dd/mm/yyyy). What am I doing wrong with the above? Many thanks for any pointers. -- updated regex above shortly after posting - apologies for inconvenience -- I think you should escape the slashes /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$

Are preg_match() and preg_replace() slow?

*爱你&永不变心* 提交于 2019-12-05 02:30:21
I've been coding in PHP for a while and I keep reading that you should only use preg_match and preg_replace when you have to because it slows down performance. Why is this? Would it really be bad to use 20 preg_matches in one file instead of using another PHP function. As Mike Brant said in his answer: There's nothing wrong with using any of the preg_* functions, if you need them. You want to know if it's a good idea to have something like 20 preg_match calls in a single file, well, honestly: I'd say that's too many. I've often stated that "if your solution to a problem relies on more than 3

How to get text in array between all <span> tag from HTML?

谁说胖子不能爱 提交于 2019-12-05 01:36:14
I want to fetch text in array between all <span> </span> tag from HTML, I have tried with this code but it returns only one occurrence : preg_match('/<span>(.+?)<\/span>/is', $row['tbl_highlighted_icon_content'], $matches); echo $matches[1]; My HTML: <span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way, My code returns only one occurrence of span tag, but I want get all text from every span tag in HTML in

PHP: Simple regular expressions to match length?

*爱你&永不变心* 提交于 2019-12-05 00:49:58
I'm creating a registration system that needs to check the name/pass etc. with REGEX (and prefer to), what I've got so far is: //Check so numbers aren't first, such as 00foobar preg_match('/^(?!\d)[a-z0-9]+$/iD',$usrname); //Just simple check preg_match('/^[a-zA-Z0-9]+$/',$psword); But I have to do stupid things in IF statements like: if strlen($psword) > 30 || if (strlen($psword) < 4) .... How would I impliment the length checking in my two original regular expression statements? This would make me so happy.. RageZ same but using the \w and \d for word and digits, but you might want also to

preg_match function for phone numbers

核能气质少年 提交于 2019-12-04 21:57:37
I have some troubles matching a phone number with this function return (bool)preg_match( '/^[\-+]?[0-9]+$/', $num); The above function always returns false after stripping brackets ( ) and dash - am left with +1234567890 what am i missing Test Here: https://eval.in/84659 $p = '/[\-+]?[0-9]+$/'; $num ='+1234567890'; echo preg_match( $p, $num)." "; $num1 ='+1234567890d'; echo preg_match( $p, $num1)." "; $num1 ='-1234567890'; echo preg_match( $p, $num1)." "; OUTPUT: 1 0 1 You probably have some whitespace in the string. Try using trim return (bool)preg_match( '/^[\-+]?[0-9]+$/', trim($num)); For

How would I compare two text files for matches with PHP

谁都会走 提交于 2019-12-04 19:21:23
$domains = file('../../domains.txt'); $keywords = file('../../keywords.txt'); $domains will be in format of: 3kool4u.com,9/29/2013 12:00:00 AM,AUC 3liftdr.com,9/29/2013 12:00:00 AM,AUC 3lionmedia.com,9/29/2013 12:00:00 AM,AUC 3mdprod.com,9/29/2013 12:00:00 AM,AUC 3mdproductions.com,9/29/2013 12:00:00 AM,AUC keywords will be in format of: keyword1 keyword2 keyword3 I guess I would really like to do an array for keywords from a file and search each line of domains.txt for matches. Not sure where to start as I'm confused at the difference of preg_match, preg_match_all, and strpos and more or less

How can I adapt my regex to allow for escaped quotes?

百般思念 提交于 2019-12-04 17:58:14
Introduction First my general issue is that I want to string replace question marks in a string, but only when they are not quoted. So I found a similar answer on SO ( link ) and began testing out the code. Unfortunately, of course, the code does not take into account escaped quotes. For example: $string = 'hello="is it me your are looking for\\"?" AND test=?'; I have adapted a regular expression and code from that answer to the question: How to replace words outside double and single quotes , which is reproduced here for ease of reading my question: <?php function str_replace_outside_quotes(

Get youtube id for all url types

别来无恙 提交于 2019-12-04 17:49:26
The following code works with all YouTube domains except for youtu.be . An example would be: http://www.youtube.com/watch?v=ZedLgAF9aEg would turn into: ZedLgAF9aEg My question is how would I be able to make it work with http://youtu.be/ZedLgAF9aEg . I'm not so great with regex so your help is much appreciated. My code is: $text = preg_replace("#[&\?].+$#", "", preg_replace("#http://(?:www\.)?youtu\.?be(?:\.com)?/(embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=|)#i", "", $text)); } $text = (htmlentities($text, ENT_QUOTES, 'UTF-8')); Thanks again! Your regex appears to solve the problem as it stands

Get numbers from string using preg_match [closed]

陌路散爱 提交于 2019-12-04 17:44:29
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have a string : <div id="post_message_957119941"> I want to fetch only the numbers (957119941) from this string using preg_match . 回答1: This shouldn't be too hard. $str = '<div id="post_message_957119941">'; if

preg match for repeating or incremental characters

眉间皱痕 提交于 2019-12-04 14:32:35
I am using preg match to validate password as: (preg_match("/^.*(?=.{5,}).*$/", $password) which accept special characters too. but for some reason, i need to modify it which should accept only alphanumeric minimum 5 characters long and very important that it must not have any repeating or incremental characters like: aaaa or 12345 or abc123 etc . if (preg_match( '%^ # Start of string (?!.*(.)\1) # Assert no repeated characters # Assert no sequential digits/characters (?!.*(?:01|12|23|34|45|56|67|78|89|90| ab|bc|cd|de|ef|fg|gh|hi|ij|jk| kl|lm|mn|no|op|pq|qr|rs|st|tu| uv|vw|wx|xy|yz)) [a-z0-9]