PHP - Regexp W/ Preg_match
EDIT! I just figured out that RegExp has it's own Forum. I apologize!
I have decided it may be quicker to seek help with this particular problem. I really, really, really hate regexp with a passion. It's one thing for JS, another for PHP, and for some reason I just can't seem to get them set up to do what I want properly. So I'm just going to leave it up to people who probably have more experience with them or just all around smarter than I am. Here is a list of RegExp that I am using and what it is "SUPPOSED" to do. Code: [Select] <?php // Check username to make sure it only has Letters and Numbers preg_match("/^[-a-zA-Z0-9]+$/", $uname); // Check Password to make sure it has 2 Numbers preg_match("/(.*[0-9].*[0-9])/", $pass); // Check Password to make sure it has 2 Special Chars from the List preg_match("/(.*[!,@,#,$,%,^,&,*,?,-,_,~].*[!,@,#,$,%,^,&,*,?,-,_,~])/", $pass); // Check if Email is in Valid Format preg_match("/^(?i)[0-9a-z]([-.\\w]*[0-9a-z])*@([0-9a-z][-\\w]*[0-9a-z]\\.)+[a-z]{2,9}\$/", $email); //Check to make sure First Name has only letters, hyphens, or an apostrophes! preg_match("/^(?i)[-a-z']$/", $fname); // Check to make sure Last Name has only letters, hyphens, or apostrophes! preg_match("/^(?i)[-a-z']$/", $lname); ?> Now each and every single one of these features works properly in Javascript, unfortunately I can't quite figure out how to get them to work properly in PHP. I use ( ! ) to check if there isn't a match, but the RegExp is giving me a bit of a headache. What should be valid comes back as invalid. I can't remember which I actually did, and which I just copied or found somewhere else. I just want the torture to stop! Can anyone help? Similar TutorialsThis topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=347542.0 This is the first time Im trying to use REGEXP, but cant get it working. It gives no error, just nothing..Anyone got idea why, please? Thanks <?php include ('connectToDatabase.php'); $getSearch = $_REQUEST['getSearch']; $keywordSearchQuery = "SELECT shoeName, shoeSize, colour, price, gender, description WHERE shoeName REGEXP '.*($getSearch).*' || shoeSize REGEXP '.*($getSearch).*' || colour REGEXP '.*($getSearch).*' || price REGEXP '.*($getSearch).*' || gender REGEXP '.*($getSearch).*' || description REGEXP '.*($getSearch).*" ; $keywordSearchQueryResult = mysql_query($keywordSearchQuery); echo "$keywordSearchQueryResult"; ?> I saw an example when i was learning pcre recently. It is in dot chapter and its address is at http://php.net/manua...ference.dot.php. The codes like this:
Consider, preg_match_all("/<img.*>/", $htmlfile, $match); preg_match_all("/(*ANY)<img.*>/", $htmlfile, $match); Now, any character that could possibly be seen as a newline will be interpreted as a newline by the PCRE.My question is "Does the dot character in pattern preg_match_all("/(*ANY)<img.*>/", $htmlfile, $match) really match newline?" I have thought quite some time but still don't know why. Can anyone help solve this question for me? Thanks! Folks, I am just trying to learn PHP. For form input validation which is better - Regexp or PHP Filters? Or do they have completely different uses? Where does preg fit in? Thank you! J.S. hi guys, i am trying to create a button that when u press if in the text are will appear <video>...</video> i know this is js do don't worry i will manage to do it, my problem is in php, i did a function that recognizes the link between <video>...</video> and then it embades it and shows me a youtube video instead, the problem i got is that if i have in the text area more <video>...</video>, like: bla bla bal<video>youtube link</video> and the other vid <video>youtube link</video> i dont know how to make it embade both, i dont know how to check if there is more, and the second problem i got is that all the text that was not betwen <video>...</video> will disapear after the vide is embadded, here is the code i done until now Code: [Select] <form action="test1.php" method = "POST"> <textarea name="body" id="text" cols="30" rows="10"></textarea> <input type="submit" value = "submit"> </form> <?php function video($string){ preg_match_all("#<video\b[^>]*>(.*?)</video>#", $string, $output); preg_match_all("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $output[0][0], $match); return "<iframe width='560' height='315' src='http://www.youtube.com/embed/".$match[0][0]."' frameborder='0' allowfullscreen></iframe>"; } if (isset($_POST['body'])&&!empty($_POST['body'])) { $body = $_POST['body']; print_r(video("$body")); } ?> Hi there, im looking for a little bit of help, could someone who knows how to use REXEXP properly tell me how i would select only the uppercase titles from title column and display them Code: [Select] +------------+-----------+ | id | title | +------------+-----------+ | 1 | one | | 2 | TWO | | 3 | three | | 4 | FOUR | +------------+-----------+ Code: [Select] <?php $username="user"; $password="pw"; $database="db"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT title FROM template WHERE name REGEXP '^(A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z)'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "Custom Template Name : {$row['title']} <br><br>"; } mysql_close(); ?> This was an attempt i made but it did not work i got the following error: Quote Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\ProgramData\xampp\htdocs\test.php on line 14 I did attempt to do a search on it but if im honest i didnt fully understand what they were saying. Im using php 5.3.5 and mysql 5.5.8 Thanks for your help Hi Guys I am having trouble with a mysql regexp expression called through php. I am not sure but I suspect its to do with the {} in the mysql code and PHP parsing them incorrectly. Here is my code snippet: $q = mysql_real_escape_string($_POST['keyword']); $limit = 10; //limit number of responses from dictionary $remainder = 11 - strlen($q); if ($q) { $qy = "SELECT * FROM dict_list WHERE UCASE(word) <> UCASE('$q') AND (word REGEXP '^$q.{$remainder}') LIMIT $limit"; $query = mysql_query($qy); ... What I want to do is this: a user types in a word. I then want to query a dictionary table called dict_list for all matches of the this word up to 11 characters max. Example: if $q was the word aero then the response would be all words beginning with aero and up to a max of 11 characters. That is why IU thought I could calculate length of $q and from this get the $remainder = 11 - strlen($q) then in the REGEXP use .{$remainder} but when testing if I echo out the query $qy the curly braces will not show on screen. Any help would be greatly appreciated. I hope this is posted in correct location. Apologies if not. Thanks, Fergal. Hi im trying to do a registration form and i am new to using preg_match. I keep getting the error "( ! ) Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\wamp\www\registration1.php on line 67" and for every other line I use preg_match. this is an example of how i have used it Code: [Select] if ( preg_match('^[[:alnum:]\.\'\-]{4,50}$', stripslashes(trim($_POST['UserName']))) ) { $UserName = mysql_real_escape_string($_POST['UserName']); $query = "SELECT UserName FROM Users WHERE UserName = '$UserName'"; $result = @mysql_query($query); $num = @mysql_num_rows($result); im such a noob at this can someone please help me I would also like to add that I have read the http://www.php.net/manual/en/function.preg-match.php page but i dont really understand it. i'm trying to grab the file name from mediafire to my database caption, I have rapidshare working fine. here's the code for rapidshare Code: [Select] if($type==1) { $words=$links[$j]; preg_match("/rapidshare\.com\/files\/\d+\/(.+)/",$words,$match); unset($words); $words=$match[1]; unset($match); $words=preg_split("/[_\.\-]/",$words); $lastword=array_pop($words); if($lastword=="html") array_pop($words); $words=implode(" ",$words); $words=preg_replace("/\s{2,}/"," ",$words); $caption=mysql_real_escape_string($words); unset($words); }How ever, i'm not sure which preg_match would be the correct one for mediafire.. appreciate any help. I'm pretty new to this, How exactly could i get the text between the www. and .com? so if i have a url: http://www.facebook.com I want to just return "facebook" Hi, I have the following code which FTPs to a site, gets the source code for a HTML file and then puts the data inbetween <TITLE> and </TITLE> into an array and this works fine: <?php $file = file_get_contents("ftp://user:pass@domain/public_html/index.html", "r"); if (!$file) { echo "<p>Unable to open remote file.\n"; exit; } preg_match_all ('/<TITLE>(.*)<\/TITLE>/msU', $file, $matches, PREG_SET_ORDER); print_r ($matches); ?> Rather than finding a match for data between <TITLE> and </TITLE> however I would like it to pull back what is inbetween the following: <!--START--> and <!--END--> I have tried the following but it doesn't put anything into the array: preg_match_all ('/<!--START-->(.*)<\/!--END-->/msU', $file, $matches, PREG_SET_ORDER); This is the content of the html file if this helps: Code: [Select] <TITLE>The Title</TITLE> <!--START--><p><strong>Some Text</strong></p> <p><strong><img src="images/IMAG0152_2.jpg" alt="" width="200" height="100" /></strong></p> <!--END--> i can get a match on stuff between TITLEs but not START and END.. I'm not sure how to change preg_match_all to work!! i've tried loads of combinations but can't get any to work, but i'm not really sure on modifiers and patterns or escaping etc, if anyone could help that would be great. Thanks in advance kev. I have a DOMDocument which I inspect an HTML page for links (href) That part works however I am not sure how to use preg_match if I only want to display a link in this layout: <tr> <td headers="c1"><a title="Link to entity information." tabindex="1" href="CORPSEARCH.ENTITY_INFORMATION?p_nameid=3236937&p_corpid=3227476&p_entity_name=%41%72%77%65%6E%20%45%71%75%69%74%69%65%73&p_name_type=%41&p_search_type=%42%45%47%49%4E%53&p_srch_results_page=0">ABC LLC</a></td> </tr> I hope someone is really good at preg_match to help me. Thanks. Im trying to match this string, but i dont wat to match normal words like "hello world 646" Code: [Select] $string = "2ab055843241f20044af82ae0d5ace04"; //always over 15 chars - without spaces if ( preg_match('~^[0-9\s]+$~D', $string)) { echo 'yes'; } If i can do it with preg_match that would be great Why dosent this work, im trying to match upper case, lower case and numbers only Code: [Select] $id = '3xDJ7@#'; if (!preg_match('/^[a-zA-Z0-9]/', $id)){ echo "ERROR!"; }else{ echo 'ok'; } This always echos "ok" Hi guys, I have been using this code: preg_match('/<h2>(.*?)<\/h2>/', $data, $matches); which i changed to: $tag1 = 'h2'; $tag2 = 'h2'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); however i need it to work allowing variables (of any character or symbol) within $tag1. I tried $tag1 = 'h(.*?)2'; $tag2 = 'h2'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); but that does not work .... essentiall the whole thing would work like this (example only): firstpartoftag1VARIABLE1lastpartoftag1VARIABLE2tag2 ps the preg_match should only capture the VARIABLE2 data not VARIABLE1 any ideas or help would be much appreciated cheers in advance I need to select an array item based on my preg result. for example: Code: [Select] $content = 'test-1'; $match = preg_replace('|test-([0-9])|', $array['\\1'], $content); is this possible? im trying to make a script so i can get users details like longitude, latitude etc
but im having trouble matching the html by regular expression
here is the html below that im trying to extract the data from:
any help or possible any alternative, suggested ways would be greatful thank you
<td>Country:</td> <td><img src="/images/dot.gif" class="flag-16 gb" align="absmiddle" width="16" height="16" title="United Kingdom"> United Kingdom (GB)</td> </tr> <tr> <td>City:</td> <td>Newport</td> </tr> <tr> <td>Region:</td> <td>Newport</td> </tr> <tr> <td>Latitude:</td> <td>51.5833</td> </tr> <tr> <td>Longitude:</td> <td>-2.9833</td> </tr> <tr> <td>Timezone:</td> <td>Europe/London</td> <?php $url = "http://smart-ip.net/geoip/2.101.108.124/"; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $rawdata = curl_exec($ch); curl_close($ch); if (preg_match('/<td>Country:</td><td>[img](?P<country>\w+)</td>/$', $rawdata, $match)) { $country = $match['country']; } if (preg_match('/<td>City:</td><td>(?P<city>\w+)</td>/$', $rawdata, $match)) { $city = $match['city']; } if (preg_match('/<td>Region:</td><td>(?P<region>\w+)</td>/$', $rawdata, $match)) { $region = $match['region']; } if (preg_match('/<td>Latitude:</td><td>(?P<latitude>\d+)</td>/$', $rawdata, $match)) { $latitude = $match['latitude']; } if (preg_match('/<td>Longitude:</td><td>(?P<longitude>\d+)</td>/$', $rawdata, $match)) { $longitude = $match['longitude']; } if (preg_match('/<td>Timezone:</td><td>(?P<timezone>\w+)</td>/$', $rawdata, $match)) { $timezone = $match['timezone']; } ?> Hey guys, In my contact form i have the action: preg_match but i've come accross a problem. Problem: Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\Program Files (x86)\wamp\www\ts\contact.php on line 34 Code:$error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; Thanks in advance I know $regex_pattern is wrong but I need a solution to find a content between <body> and </body>. Code: [Select] <?php $content = "<html><head><title>Your IP</title></head><body>Your IP Address: 63.1.142.154</body></html>"; $regex_pattern = "/<body>(.*)<\/body>/"; $preg_match($regex_pattern,htmlspecialchars($content),$matches); print_r( $matches ); ?> I've tried alot of different ways, google, but just can't find the correct way to do this.. I'm trying to search for a word in an inputted string.. here's the code <html> <head><title>TESTING</title></head> <body> <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"/> email: <input type=text name=email value="" /><br/> feedback: <input type=text name=ex value=""/><br/> <input type="submit" value="submit" /><br/> </body> </html> <?php $email = $_GET['email']; $ex = $_GET['ex']; $pattern = '/^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z]+$/'; //$pattern1 = '/nprezident/prezident/'; if(!preg_match($pattern, $email)){ echo 'invalid email address<br/>'; } else { echo 'good to go<br/>'; } if(preg_match('/nprezident/,/prezident/', $ex)){ echo 'just something about that sentence is nice'; } else { echo 'no good'; } ?> Ok the one I'm having a problem with is the second preg_match when trying to search for two words i can get one word to print but when i use add a second word it gives an error. How do i add a second word? |