preg-match-all

Php Regex to find if string is mysql select statement

▼魔方 西西 提交于 2019-12-10 18:35:54
问题 I'm trying to validate queries before executing them, If query is not a mysql select statement then i have to show message to user. I found below regex from this link: Validate simple select query using Regular expression $reg="/^Select\s+(?:\w+\s*(?:(?=from\b)|,\s*))+from\s+\w+\s+where\s+\w+\s*=\s*'[^']*'$/i"; next i wrote below code but it always prints not select query ($match is empty every time) $string="select * from users where id=1"; preg_match_all($reg,$string,$match); if(!empty(

Php regular expression to match a div

你说的曾经没有我的故事 提交于 2019-12-10 14:23:10
问题 This is mycode <?php /** * @author Joomlacoders * @copyright 2010 */ $url="http://urlchecker.net/html/demo.html"; $innerHtml=file_get_contents($url); //echo $innerHtml; preg_match_all("{\<div id='news-id-.*d'\>(.*)\</div\>}",$innerHtml,$matches); //<div id='news-id-160346'> var_dump($matches); ?> I want find all content in div id='news-id-160346'. Please help me 回答1: Use an HTML parser. NOT regular expressions. The problem with regular expressions is that they cannot match nested structures.

PHP: How do I get the string indexes of a preg_match_all?

本小妞迷上赌 提交于 2019-12-10 04:37:10
问题 let's say I have two regexp's, /eat (apple|pear)/ /I like/ and text "I like to eat apples on a rainy day, but on sunny days, I like to eat pears." What I want is to get the following indexes with preg_match: match: 0,5 (I like) match: 10,19 (eat apples) match: 57,62 (I like) match: 67,75 (eat pears) Is there any way to get these indexes using preg_match_all without looping through the text every single time? EDIT: SOLUTION PREG_OFFSET_CAPTURE ! 回答1: You can try PREG_OFFSET_CAPTURE flag for

Regex - PHP Lookaround

穿精又带淫゛_ 提交于 2019-12-09 18:38:44
问题 I have a string, such as this: $foo = 'Hello __("How are you") I am __("very good thank you")' I know it's a strange string, but stay with me please :P I need a regex expression that will look for the content between __("Look for content here") and put it in an array. i.e. the regular expression would find "How are you" and "very good thank you". 回答1: Try this: preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches); print_r($matches); which means: (?<= # start positive look behind __\(" #

How to write nested regex to find words below some string?

試著忘記壹切 提交于 2019-12-09 08:02:29
I am converting one pdf to text with xpdf and then find some words with help of regex and preg_match_all. I am seperating my words with colon in pdftotext. Below is my pdftotext output: In respect of Shareholders Name: xyx Residential address: dublin No of Shares: 2 Name: abc Residential address: canada No of Shares: 2 So i write one regex that will show me words after colon in text(). $regex = '/(?<=: ).+/'; preg_match_all($regex, $string, $matches); But Now i want regex that will display all data after In respect of Shareholders . So, i write $regex = '/(?<=In respect of Shareholders).*?(?=

Get all matches from string that start with and end, using php

烈酒焚心 提交于 2019-12-09 02:51:44
问题 How would I use PHP to extract everything in between [startstring] and [endstring] from this string: [startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] welcome to the universe I'd like to find all the matches of [startstring] and [endstring] , and echo the text inside. Kinda like a delimiter. And I would like all the matches echoed to be seperated by a space. How would I go about accomplishing this? Would this require regex ? Could you provide a sample? Thanks

preg_match_all

旧时模样 提交于 2019-12-08 12:26:48
问题 Could some one explain this to me i am not familiar with preg_match_all filters, this code works fine except it does not return a negative value if one of the latitudes and longitudes is negative. if ( preg_match_all( "#<td>\s+-?(\d+\.\d+)\s+</td>#", $output, $coords ) ) { list( $lat, $long ) = $coords[1]; echo "Latitude: $lat\nLongitude: $long\n"; } output: Latitude: 30.6963 Longitude: 71.6207 (longitude is missing a '-') 回答1: The value of the coords variable depends on what is matched by

PHP preg_match_all html

删除回忆录丶 提交于 2019-12-08 09:39:53
问题 how can i create a preg_match_all regex pattern for php to give me this code? <td class="class2"> </td> <td class="class2" align="right"><span class="DarkText">I WANT THIS TEXT</span></td> To get me the text inside the span class? thanks! 回答1: You can use: preg_match_all("!<span[^>]+>(.*?)</span>!", $str, $matches); Then your text will be inside the first capture group (as seen on rubular) With that out of the way, note that regex shouldn't be used to parse HTML. You will be better off using

PHP REGEX: Ignore + and #

混江龙づ霸主 提交于 2019-12-08 06:17:16
问题 So here is what I'm doing exactly: I have a form with a caption on the right of it, I want to write in the form only A-Z,0-9 and whitespaces, and for the caption I want to do the opposite, so if the user write's something wrong I can show what's the problem for example: "Invalid Charachter" But I'm stuck with + and # I want to ignore them too from the form with regular expression so I can show the "Invalid character" message for these too, as I saw php thinks that + sign is = to space ( ) or

Regex to get content between single and double quotes php

与世无争的帅哥 提交于 2019-12-07 23:25:33
问题 I have code like: preg_match_all(UNKNOWN, "I need \"this\" and 'this'", $matches) I need the REGEX that would have the $matches return just two entries of “this” without quotes. 回答1: I think following should work: $str = 'I need "this" and \'this\''; if (preg_match_all('~(["\'])([^"\']+)\1~', $str, $arr)) print_r($arr[2]); OUTPUT: Array ( [0] => this [1] => this ) 回答2: preg_match_all('/"(.*?)".*?\'(.*?)\'/', "I need \"this\" and 'this'", $matches); But note that the order of quoted strings