preg-match

Find all PHP Variables with preg_match

拥有回忆 提交于 2019-12-10 16:16:37
问题 How can I find all PHP variables with preg_match . I made the following regular expression: $string = 'Hallo $var. blabla $var, $iam a var $varvarvar gfg djf jdfgjh fd $variable'; $instring = array(); preg_match_all('/\$(.*?)/', $string, $instring); print_r($instring); I just don't understand how regular expressions work. 回答1: \$(.*?) Is not the right regular expression to match a PHP variable name. Such a regular expression for a Variable Name is actually part of the PHP manual and given as

PHP Regular Expression [accept selected characters only]

[亡魂溺海] 提交于 2019-12-10 16:07:57
问题 I want to accept a list of character as input from the user and reject the rest. I can accept a formatted string or find if a character/string is missing. But how I can accept only a set of character while reject all other characters. I would like to use preg_match to do this. e.g. Allowable characters are: a..z, A..Z, -, ’ ‘ User must able to enter those character in any order. But they must not allowed to use other than those characters. 回答1: Use a negated character class: [^A-Za-z-\w] This

Url Matching using Gruber's regex in PHP

偶尔善良 提交于 2019-12-10 14:23:51
问题 how do I get the regex mentioned in this article working with preg_match in php? <?php preg_match("\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))/i", $text, $matches); print_r($matches); ?> Using the code above I get the following error: Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash... 回答1: Try this: preg_match("#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#i", $text, $matches); You were missing the

Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset

妖精的绣舞 提交于 2019-12-10 14:21:00
问题 i'm trying to change the preg_match check from url checking to username checking which is min/max 2-16chrs, dash, space & hypen acceptable. i'm getting this error Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 14 if(empty($string) || preg_match("#^([\w- ]{2,16}*(?:.[\w- ]{2,16}*)+):?(d+)?/?#i", $string)) old code that looked for URL if(empty($string) || preg_match("#^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i",

How to read a text file and search for a certain string before a colon and then show the content after the colon?

独自空忆成欢 提交于 2019-12-10 14:05:50
问题 I have a file that contains something like this: test:fOwimWPu0eSaNR8 test2:vogAqsfXpKzCfGr I would like to be able to search the file for say test and it set the string after the : to a variable so it can be displayed, used etc. Here is the code I have so far for finding 'test' in the file. $file = 'file.txt'; $string = 'test'; $searchFile = file_get_contents($file); if (preg_match('/\\b'.$string.'\\b/', $searchFile)) { echo 'true'; // Find String } else { echo 'false'; } How would I go

Help with regex to match any url but that of admin folder

爷,独闯天下 提交于 2019-12-10 13:59:10
问题 I am stuck with something I know must be simple but been going round in circles for a while now. I want to create a regular expression for use with a cms routing feature using php and preg_match that matches any URL that doesn't begin with the string 'admin/' as somedomain.com/admin/ will be the admin part of the cms. I have tried so far preg_match('#^([^admin])+(.+)$#', 'mens-books')) which fails as the url string 'mens-books' I am looking for is a match against the letter m in the string

Using preg_match to find all words in a list

☆樱花仙子☆ 提交于 2019-12-10 13:49:13
问题 With help from SO, I was able to pull a 'keyword' out of an email subject line to use as a category. Now I've decided to allow multiple categories per image, but can't seem to word my question properly to get a good response from Google. preg_match stops at the first word in the list. I'm sure this has something to do with being 'eager' or simply replacing the pipe symbol | with something else, but I just can't see it. \b(?:amsterdam|paris|zurich|munich|frankfurt|bulle)\b . The whole string I

php validate string with preg_match

霸气de小男生 提交于 2019-12-10 13:32:16
问题 I am trying to verify in PHP with preg_match that an input string contains only "a-z, A-Z, -, _ ,0-9" characters. If it contains just these, then validate. I tried to search on google but I could not find anything usefull. Can anybody help? Thank you ! 回答1: Use the pattern '/^[A-Za-z0-9_-]*$/' , if an empty string is also valid. Otherwise '/^[A-Za-z0-9_-]+$/' So: $yourString = "blahblah"; if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) { #your string is good } Also, note that you want to

PHP preg_match length 3276 limit

眉间皱痕 提交于 2019-12-10 13:22:58
问题 It appears that PHP's preg_match has a 3276 character limit for matching repeating characters in some cases. i.e. ^(.|\s){0,3276}$ works, but ^(.|\s){0,3277}$ does not. It doesn't seem to always apply, as /^(.){0,3277}$/ works. I can't find this mentioned anywhere in PHP's documentation or the bug tracker. The number 3276 seems a bit of an odd boundary, the only thing I can think of is that it's approximately 1/10th of 32767, which is the limit for a signed 16-bit integer. preg_last_error()

preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 27 on line number 25

可紊 提交于 2019-12-10 12:36:10
问题 I am writing up some PHP code. In this code, I am running a for loop within a for loop to iterate over an array, then to iterate over the characters in the current string in the array. I then want to do preg_match() on the current string to see if it matches a rather ling RegEx. preg_match('/[ \f\n\r\t\v\x{00a0}\x{1680}\x{180e}\x{2000-}\x{200a}\x{2028}\x{2029}\x{202f}\x{205f}\x{3000}\x{feff}]/', $input[$i][$j]) But I keep on receiving the following error: WARNING preg_match(): Compilation