preg-match

Matching data from file with regex

試著忘記壹切 提交于 2020-01-04 14:00:45
问题 The file I am pulling the data from consists of the following information <"DATA" 10.21 ^"DATA" 81.39 _"DATA" 38.71 "DATA" 84.19 Using preg_match, how can I pull the values from each? I tried $r = '/<"DATA" (.+?)/'; but it didn't give me the numbers. Anyone know the correct regex to pull these numbers? Thanks in advance! 回答1: preg_match_all('/^\s*.?"DATA" (\d+)\.(\d+)\s*$/m', $str, $matches); CodePad. 回答2: You have to use the preg_match_all function: preg_match_all('/^[<^_ ]"DATA" (\d+\.\d+)$

preg_match and long strings

99封情书 提交于 2020-01-04 01:08:39
问题 This is the preg_match i am trying to use to find specific text in text file. if (preg_match($regexp,$textFile,$result) > 0) { echo "Found ".$result[0]; } else { echo "Not found"; } However, the result is always Found and nothing more. The result array is empty. Now i read that preg_match can't work with long strings. My text file is about 300KB so thats 300000 characters i guess. I am 100% sure that the searched string is in the text file, and the fact that preg_match function returns value

preg_match and long strings

折月煮酒 提交于 2020-01-04 01:08:06
问题 This is the preg_match i am trying to use to find specific text in text file. if (preg_match($regexp,$textFile,$result) > 0) { echo "Found ".$result[0]; } else { echo "Not found"; } However, the result is always Found and nothing more. The result array is empty. Now i read that preg_match can't work with long strings. My text file is about 300KB so thats 300000 characters i guess. I am 100% sure that the searched string is in the text file, and the fact that preg_match function returns value

Get more backreferences from regexp than parenthesis

 ̄綄美尐妖づ 提交于 2020-01-03 02:22:14
问题 Ok this is really difficult to explain in English, so I'll just give an example. I am going to have strings in the following format: key-value;key1-value;key2-... and I need to extract the data to be an array array('key'=>'value','key1'=>'value1', ... ) I was planning to use regexp to achieve (most of) this functionality, and wrote this regular expression: /^(\w+)-([^-;]+)(?:;(\w+)-([^-;]+))*;?$/ to work with preg_match and this code: for ($l = count($matches),$i = 1;$i<$l;$i+=2) {

PHP to SEARCH the Upper+Lower Case mixed Words in the strings?

让人想犯罪 __ 提交于 2020-01-02 13:26:15
问题 Lets say there is a string like: A quick brOwn FOX called F. 4lviN The WORDS i want TO SEARCH must have the following conditions: The words containing MIXED Upper & Lower Cases Containing ONLY alphabets A to Z (A-Z a-z) (e.g: No numbers, NO commas, NO full-stops, NO dash .. etc) So suppose, when i search (for e.g in this string), the search result will be: brOwn Because it is the only word which contains both of Upper & Lower Case letters inside (and also containing only alphabets). So how

PHP to SEARCH the Upper+Lower Case mixed Words in the strings?

半城伤御伤魂 提交于 2020-01-02 13:26:07
问题 Lets say there is a string like: A quick brOwn FOX called F. 4lviN The WORDS i want TO SEARCH must have the following conditions: The words containing MIXED Upper & Lower Cases Containing ONLY alphabets A to Z (A-Z a-z) (e.g: No numbers, NO commas, NO full-stops, NO dash .. etc) So suppose, when i search (for e.g in this string), the search result will be: brOwn Because it is the only word which contains both of Upper & Lower Case letters inside (and also containing only alphabets). So how

Get keyword from a (search engine) referrer url using PHP

余生颓废 提交于 2020-01-02 02:59:26
问题 I am trying to get the search keyword from a referrer url. Currently, I am using the following code for Google urls. But sometimes it is not working... $query_get = "(q|p)"; $referrer = "http://www.google.com/search?hl=en&q=learn+php+2&client=firefox"; preg_match('/[?&]'.$query_get.'=(.*?)[&]/',$referrer,$search_keyword); Is there another/clean/working way to do this? Thank you, Prasad 回答1: If you're using PHP5 take a look at http://php.net/parse_url and http://php.net/parse_str Example: //

Are preg_match() and preg_replace() slow?

徘徊边缘 提交于 2020-01-02 01:44:08
问题 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. 回答1: 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,

PHP: Simple regular expressions to match length?

拜拜、爱过 提交于 2020-01-02 01:02:33
问题 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

How would I compare two text files for matches with PHP

爱⌒轻易说出口 提交于 2020-01-01 19:33:13
问题 $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