preg-match

How do you perform a preg_match where the pattern is an array, in php?

I have an array full of patterns that I need matched. Any way to do that, other than a for() loop? Im trying to do it in the least CPU intensive way, since I will be doing dozens of these every minute. Real world example is, Im building a link status checker, which will check links to various online video sites, to ensure that the vid...

Replacing Tags with Includes in PHP with RegExps

I need to read a string, detect a {VAR}, and then do a file_get_contents('VAR.php') in place of {VAR}. The "VAR" can be named anything, like TEST, or CONTACT-FORM, etc. I don't want to know what VAR is -- not to do a hard-coded condition, but to just see an uppercase alphanumeric tag surrounded by curly braces and just do a file_get_cont...

Delimiter in one string

Hello, Here is an example of a file that i use to create a mysql db. The delimiter is "," but in the description for a single column exist ",". Header: City State Zip Description Los Angeles, California , 98005, "welcome to california, were are living the dream","Please stay, a while." The problem is that the description in "quotes"...

Need regex to parse keyword='value' with single or double quotes

I'm having trouble writing a regular expression (suitable for PHP's preg_match()) that will parse keyword='value' pairs regardless of whether the <value> string is enclosed in single or double quotes. IOW in both of the following cases I need to get the <name> and <value> where the <value> string may contain the non-enclosing type of quo...

Regex Optional Groups?

I seem to have confused myself with a preg_match regex I'm doing, so fresh eyes and help would be appreciated. My current regex is as follows: /<!--menu:start:\(([0-9])\,([0-9])\)-->(.*?)<!--menu:end-->/se I am looking to make the number input and colon e.g. :(1,4) optional, so it would match: <!--menu:start--><!--menu:end--> or ...

How do I use PHP's preg_match to validate strings?

Please tell me how to use the various symbols used for expression matching in preg_match function of PHP. Besides basic information, please give example for checking a valid e-mail address and a string that should not contain / : ; { } * & ...

Regex for finding valid filename

I want to check whether a string is a file name (name DOT ext) or not. Name of file cannot contain / ? * : ; { } \ Could you please suggest me the regex expression to use in preg_match()? ...

Preg Match circumflex ^ in php

I cant quite get my head around what the ^ is doing in my preg_match. if (preg_match('~^(\d\d\d\d)(-(\d{1,2})(-(\d{1,2}))?)?$~', trim($date), $dateParts)) { echo the $dateparts and do some magic with them } else { tell me the date is formatted wrong } As I see it this is looking to see if the $date matches the format which I ...

PHP preg_match question

How can I match the three words in the following string with a Perl compatible regular expression? word1#$word2#$word3 I don't know the actual words "word1, word2 and word3" in advance. I only know the separator, which is #$. And I can't use the word boundary as I have a multibyte encoding. This means for instance that the string can ...

Possible preg match miss format in php

I have this code: //fetch data $data = $_POST['list']; echo($data); echo('<br>then<br>'); $data = str_replace("\t", " ", $data); echo($data); $matches = array(); $user = array(); preg_match( "/(.+?) ((?:[A-Z])(?:[0-9]+:){3}[0-9]+) ([0-9]+) \/([0-9]+) ([0-9]+) \/ ([0-9]+)/", $data, $matches ); list(,$user['base'],$user['location'...

PHP regex using preg_match and capture group

I have a string that I want to search through: "Query Result: Val for ?f : http://www.rk.com/#steak Val for ?a : http://www.rk.com/#movietheaters" The two variables that I want to capture are the two words after the hashes (in this case steak and movietheaters). Note that there will always be an uri infront of the #. I know I can easi...

SQL query and preg_match

Hi, guys! Need your help with sql query and php. I have to pieces of code here: 1. $sql = "SELECT SUBSTR(n.`title`, 1,1) FROM node n WHERE n.`type` = 'type1'"; $results = db_query($sql); while ($fields = db_fetch_array($results)) { foreach($fields as $key => $value) { echo $value; } } The code above returns first letters o...

find occurence of a set of words

Hi, I have a pattern with a small list of words that are illegal to use as nicknames set in a pattern variable like this: $pattern = webmaster|admin|webadmin|sysadmin Using preg_match, how can I achieve so that nicknames with these words are forbidden, but registering something like "admin2" or "thesysadmin" is allowed? This is the...

preg_match in preg_match_all

$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/'; $radompgr = preg_match_all($match_expression,$q2,$match, PREG_SET_ORDER); if($radompgr == TRUE){echo "found $radompgr<br>";}else{echo "not found $radompgr<br>";} //found for ($i = 0; $i < count($match); $i++) { $mathcas = $match[$i][1]; $rado...

RegEx problem - retrieve content of tag with given class - preg_match(_all)

Hi, I need to retrieve content of <p> tag with given class. Class could be simplecomment or comment ... So I wrote the following code preg_match("|(<p class=\"(simple)?comment(.*)?\">)(.*)<\/p>|ism", $fcon, $desc); Unfortunately, it returns nothing. However if I remove tag-ending part (<\/p>) it works somehow, returing the string wh...

Can preg_match() (or other php regex function) match a variable number of parenthesized subpatterns?

Suppose I have '/srv/www/site.com/htdocs/system/application/views/' and want to test it against a regexp that matches each directory name in the path? Something like this pattern: '(/[^/])' That yields an array with 'srv','www','site.com'... etc. PS: the regexp syntax I wrote is just to illustrate, it's not tested and surely wrong, but...

[PHP] Validate username as alphanumeric with underscores

On my registration page I need to validate the usernames as alphanumeric only, but also with optional underscores. I've come up with this: function validate_alphanumeric_underscore($str) { return preg_match('/^\w+$/',$str); } Which seems to work okay, but I'm not a regex expert! Does anyone spot any problem? ...

Javascript/ jQuery and regex

I"m building a registration form, and I need to validate the user's inputs. (username, email, password). I have regular expressions set up for each of these, and I can easily validate each in PHP using preg_match, and if it returns false, I can display an error. However, I think it'd be much nicer if the page did not have to refresh to...

replacing all image src tags in HTML text

Hi! I'm trying to make a simple php script to find all src attributes from all images in a html text and then replace all found srcs with some text after making some conditional changes. Something like this: @preg_match_all('/<img\s src="([a-zA-Z0-9\.;:\/\?&=_|\r|\n]{1,})"/isxmU', $body, $images); now i've all srcs into the $images ...

regex: match string only if not part of a tag

Hi, I am trying to match a string only if it is not part of an html tag. For example when searching for the string: "abc". <a href="foo.html">abc def</a> should match <p> foo bar foo abc foo bar</p> should match but <a href="abc.html">foo</a> should not match. Thanks for the help! ...