PHP - Preg Match A Url But Include Curly Brackets
Hi all
I'm quite a competent PHP coder but where it comes to preg_match I prefer to be ignorant, because I can't get my head around the patterns. Basically, I'm working with a script that someone else has written, and I'm coming across problems because the URL that is being validated sometimes has curly brackets '( )' in it (these are image file names generated by an internal system). I have the pattern, how do I modify this to allow curly brackets? On some occasions, the urls have just the opening or just the closing, so its never the complete set. if someone could give me an indication with this, because I'm clueless! Thanks in advance! Ste pattern: Code: [Select] $pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/'; Similar TutorialsHi Guys Quick noob question. I am still fairly new to oop and i'm trying to also build my first simple mvc to learn about that too. I came across some syntax use in a tutorial that works but i don't really understand why and don't know what this is called to search for it on google. I've instantiated the class and then i pull out and explode the url to make controller or function calls in the mvc. The noob bit i don't understand is this Code: [Select] $controller->{$url[1]}(); Why do the curly brackets allow you to place a variable name? And what is this type of syntax called so i can read about it. Any help would be massively appreciated! Drongo Hi There, I'm trying to get the following array to work as a separate include php file to make the HTML easier to follow. Within the HTML it has been working fine, as the "echo $tdata_consent Variable is within the main curly brackets. As soon as I move the variable outside of the brackets I only get 1 item from the array rather than all of the items. $projects_const = array(); $required_const = ['Consent']; $colors_const = array_combine($required_const, ['bg-danger text-white font-weight-bold'] ); $xml_const=simplexml_load_string($jobs_task_response) or die("Error: Cannot create object"); foreach($xml_const->Jobs->Job as $item_const) { if (in_array((string)$item_const->State, $required_const)) { $projects_const[] = array( 'job_no' => (string)$item_const->ID, 'job_name' => (string)$item_const->Name, 'job_start' => date('d/m/Y', strtotime($item_const->StartDate)), 'job_due' => date('d/m/Y', strtotime($item_const->DueDate)), 'job_status' => (string)$item_const->State, 'job_staff' => (string)$item_const->Assigned->Staff->Name, ); } } usort($projects_const, function($a,$b) {return $b['job_due'] <=> $a['job_due']; } ); foreach ($projects_const as $proj_const) { $job_no =$proj_const['job_no']; $job_name =$proj_const['job_name']; $job_status =$proj_const['job_status']; $job_staff =$proj_const['job_staff']; $job_due =$proj_const['job_due']; $clr_1 = $colors_const[$job_status]; $tdata_const = ''; $tdata_const .= "<div class='row no-gutters'>"; $tdata_const .= "<div class='col-sm bg-white border rounded'><a href='managejob.php?job_id=$job_no' class='text-left ml-1'><strong>$job_no</strong></a></div>"; $tdata_const .= "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_name</p></div>"; $tdata_const .= "<div class='col-sm-2 bg-white border rounded'>" . state_dropdown_1($job_no, $job_status, $clr_1) . "</div>"; $tdata_const .= "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_status</p></div>"; $tdata_const .= "<div class='col-sm-2 bg-white border rounded'><p class='text-left ml-1'>$job_staff</p></div>"; } echo $tdata_const; Any thoughts would be much appreciated. Thanks in Advance. so I use file_get_contents to get the contents of a webpage, one of the lines has Code: [Select] <meta property="og:url" content="http://www.google.com/blahblahblah?s=5cd04d4a7632296b9cdb463d04e82c05" /> I want it to echo only http://www.google.com/blahblahblah so anything between <meta property="og:url" content=" and ?s=5cd04d4a7632296b9cdb463d04e82c05" /> I want to extract and put into a variable this works just fine but I was thinking preg match would be better $thread = explode('<meta property="og:url" content="', $psuc); $thread = explode('?s=', $thread[1]); echo $thread[0]; Thanks Hello, I am trying to crawl a website and get all the domain names listed on the site to output into my php script. I am using the following code.... The main area is the preg match area, since I know nothing about it, nor do I understand it a little bit! $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"http://link I am getting data from.com); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result=curl_exec ($ch); curl_close ($ch); // Search The Results From The Starting Site if( $result ) { // LOOKING FOR ANYTHING.COM all .com's preg_match_all( "(.*)\.com", $result, $output, PREG_SET_ORDER ); foreach( $output as $item ) { // ALL LINKS DISPLAY HERE print_r($item); } } Any help would be greatly appreciated! Hello, I am trying to replace a url and add a sub-id to it, however I'm not quite there. Any help is always appreciated here. In the end, I would like the value of gotoUrl to have the cpId 123abc45 attached to it.
I.E.
http://example.com/?Aff=56732&SID=123abc45 http://example.com/?Aff=56732&uv=123abc45&product=1834234 http://example.com/?Aff=56732&cid=123abc45&ace=2gs244s3 $gotoUrl = "http://example.com/?Aff=56732&SID="; // OR could be listed as such: // $gotoUrl = "http://example.com/?Aff=56732&uv=default&product=1834234"; // $gotoUrl = "http://example.com/?Aff=56732&cid=&ace=2gs244s3"; // Will attach this to replace value $cpId = "123abc45"; // Possible sid replacements, depending on the url $sidList = array("sid","SID","cid","uv"); if ($gotoUrl){ foreach($sidList as $sidVar){ if (preg_match("/$sidVar/i", $gotoUrl)){ $gotoUrl = preg_replace("#{$sidVar}(?=.*\d)(?=.*[a-z])(?=.*[A-Z])$#", "{$sidVar}={$cpId}", $gotoUrl); } } echo $gotoUrl; } Edited by iwpg, 13 August 2014 - 10:36 PM. Hi all I am trying to do an email validation check. Here's my code: if (!preg_match($email) == "^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]") { include("checkout-email-error.php"); exit; } It doesn't seem to allow any email addresses through? All help greatly appreciated! Thanks Pete How do I fix this For Each statement when used with preg match? //Cat_ID & Category Name preg_match_all('%<a[^href=]*href=\"\/cats\.asp\?cat_id=(.*?)\" title="(.*?)">%',$data,$matches1,PREG_SET_ORDER); // works fine preg_match_all('%/60/(.*?).jpg"%s',$data,$matches2,PREG_SET_ORDER); // works fine preg_match_all('%h3><p>(.*?)<\/p>%s',$data,$matches3,PREG_SET_ORDER); // works fine foreach ($matches1 as $val) { //need help here $subcat_id=$val[0][1]; $subcat=$val[0][2]; $cat_image=$val[1][1]; $cat_desc=$val[2][1]; Thanks for your help!!! hey, i was wondering how i would make a preg match check for special characters, i would only like to allow an underscore _ and a dash - i'm currently using preg_match("/[^0-9]+$/",$getUser) but thats not working right This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=331482.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=326673.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=345772.0 Please help me with this code. It keep appear in my website. "Warning: preg_match() [function.preg-match]: No ending delimiter '.' found in /home/kengsite/public_html/wp-content/plugins/statpress/statpress.php on line 1184" And this is how the line look like. if (!preg_match(".ico$", $urlRequested)) { return ''; } Hi All, I am trying to create a PHP code that will run a form, however i keep getting the error message Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in D:\xampp\htdocs\HFCC\Scripts\mailprocess.php on line 55 Invalid email address entered. Go Back and try again attached is the complete copy of the code i am using, i have an email text box and label with the id 'email' also my method is POST and my action is Scripts/mailprocess.php. Please let me know if you need any additional details. Any help will be much appreciated. Thanks Hi, I want to separate out the file name from an image url. Not sure the best way to do it? I want to basically select all text after the last / slash. eg - image.jpg would be the result from http://www.google.com/images/image.jpg What's the best way to do it? Cheers Rob I didn't know how to describe this, so please excuse my title. As most of you know, this is possible: foreach($items as $key => $item) { ${$key} = $item; } // I would then be able to access it like so: $key // or whatever the value of "$key" is. However, how could I append something extra to the ${$key} variable? Instance: foreach($items as $key => $item) { $my_{$key} = $item; // or ${$key}_arr = $item; } // So that the end result of my variable becomes like so: $my_key // or $key_arr // Instead of just $key // or whatever the value of "$key" is. Is there a known method for this? Any input is appreciated, thank you. 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. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=353613.0 $text = "wow {one|two|three}fsasfa happy ness"; preg_match('/\b{*+}\b/i', $text, $matches); print_r($matches); Basically, $matches will contain "one|two|three" - but all I got is an array with "}" function send_msg($sender , $message){ if(!empty($sender) && !empty($message)){ $sender = mysql_real_escape_string($sender); $message= mysql_real_escape_string($message); $query = "INSERT INTO `chat`.`chat` VALUES (null , '{$sender}' , '$message')"; // Difficulty on THIS LINE !!!! if($run = mysql_query($query)){ return true; }else{ return false; } } why is '{$sender}' given the curley brakets ????? and why is message not given the same brackets ? also why is this function used ? i.e. mysql_real_escape_string , i know what it does , but is it to prevent SQL injection. Hi Yes I start a new thread, this is probably the correct one. |