Tag Archives: regex

Regex to check if exact string exists

Questions: I am looking for a way to check if an exact string match exists in another string using Regex or any better method suggested. I understand that you tell regex to match a space or any other non-word character at the beginning or end of a string. However, I don’t know exactly how to… Read More »

In a regex, what changes when you add a question mark to .+

Questions: I was browsing old php sources and I found a pattern I don’t understand (probably a copy/past from the internet some times ago …). Here is a simple exemple using it with php : echo preg_replace(‘#color=”(.+)”#’, ‘$1’, ‘color=”red” color=”black”‘); // Echo ‘red” color=”black’, which is fine because the (.+) try to match the largest… Read More »

Regex extract string between 2 curly braces

Questions: I have the following short codes: Dear {{name}}, You are being invited for the following event: {{event}} regards, {{author}} I have an array from the database : $data where: $data[‘name’] = ‘John Doe’; $data[‘event’] = ‘Party yay!’; $data[‘author’] = ‘Kehke Lunga’; Output that I expect: Dear John Doe, You are being invited for the… Read More »

PHP regex for url validation, filter_var is too permisive

Questions: First lets define a “URL” according to my requirements. The only protocols optionally allowed are http:// and https:// then a mandatory domain name like stackoverflow.com then optionally the rest of url components (path, query, hash, …) For reference a list of valid and invalid url’s according to my requirements VALID stackoverflow.com stackoverflow.com/questions/ask https://stackoverflow.com/questions/ask http://www.amazon.com/Computers-Internet-Books/b/ref=bhp_bb0309A_comint2?ie=UTF8&node=5&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=browse&pf_rd_r=0AH7GM29WF81Q72VPFDH&pf_rd_t=101&pf_rd_p=1273387142&pf_rd_i=283155… Read More »

PHP URL to Link with Regex

Questions: I know I’ve seen this done a lot in places, but I need something a little more different than the norm. Sadly When I search this anywhere it gets buried in posts about just making the link into an html tag link. I want the PHP function to strip out the “http://” and “https://”… Read More »

Remove multiple commas regex

Questions: Can someone please let me know how I would remove multiple commas if they were next to one another in PHP. $address= rtrim(preg_replace(‘!\s+!’, ‘ ‘, $address_line_1.’ ‘.$address_line_2.’, ‘.$town_village_city.’, ‘.$state_province_county.’, ‘.$postal_code.’, ‘.$country),’,’); //return preg_replace(‘/([!?,.])+/’,’ ‘,$address); return preg_replace(‘!\,+!’,’ ‘,$address); For example I may have an address with no town_village so end up with multiple commas. Also… Read More »

Replace some strings in PHP with regex?

Questions: I need to do a simple regex find and replace with PHP for my syntax highlighting script in PHP. I need to take a string of code which will actually be a whole php file that is read into a string like this. $code_string = ‘the whole source code of a php file will… Read More »