preg-match

PHP: search substring within a string using preg_match??

Im trying to create a function to check for a substring within a string in php. public static function stringCheck ($string, $substring) { $patern = "/".$substring."/"; if (preg_match($substring, string) { return true; } else return false; } but if i enter a special character used in preg_match (^.[$()|*+?{) it screws up ...

Scraping a messy html website with PHP

Hi all, I am in the following situation. I am trying to convert a messy scraped html code to a nice and neat xml structure. A partial HTML code of the scraped website: <p><span class='one'>week number</span></p> <p><span class='two'>day of the week</span></p> <table class='spreadsheet'> table data </table> <p><span class='two'>anoth...

Faster way to run through a couple hundred lines in a for loop and check if match exists in 200k+ line file

I have a couple hundred single words that are identified in a foreach routine and pushed into an array. I would like to check each one (word) to see if it exists in an existing txt file that is single column 200k+ lines. (Similar to a huge "bad word" routine i guess but, in the end this will add to the "filter" file.) I don't know whe...

How do I catch an invalid preg_match pattern?

I am writing a PHP script that accepts a regular expression pattern from the user which is used by preg_match(). How can I check that the pattern is valid? ...

PHP - Stop searching when match is found.

Incoming is a single word from a text box or another function function searchformatch($keyword){ if(!$keyword){hey-enter-a-keyword-function();} foreach (glob("directory/*.txt") as $filename) { $file = $filename; $contents = file($file); $string = implode($...

preg_match troubles

I am trying to match a Youtube URL with regex to see if it is valid. This is my code: if(preg_match('\bhttp://youtube.com/watch\?v=.*\b', $link)) { echo "matched youtube"; } But I'm getting an error: Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\ajax\youtube.php o...

preg_match: check birthday format (dd/mm/yyyy)

How do I make the expression which checks the birthday input to match a format like this dd/mm/yyyy? Below is what I came out so far, but it takes this too if I put 99/99/9999! if (!preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/", $cnt_birthday)) { $error = true; echo '<error elementid="cnt_birthday" message="BIRTHDAY - Only this bi...

PHP preg_match_all html

how can i create a preg_match_all regex pattern for php to give me this code? <td class="class2">&nbsp;</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! ...

Why is this regular expression not working?

Content of 1.txt: Image" href="images/product_images/original_images/9961_1.jpg" rel="disable-zoom:false; disable-expand: false"><img src="im Code that does not work: <?php $pattern = '/(images\/product_images\/original_images\/)(.*)(\.jpg)/i'; $result = file_get_contents("1.txt"); preg_match($pattern,$result,$match); echo "<h3>Preg...

check for subdomain in parse_url

Hello, I am trying to write a function to just get the users profile id or username from Facebook. They enter there url into a form then I'm trying to figure out if it's a Facebook profile page or other page. The problem is that if they enter an app page or other page that has a subdomain I would like to ignore that request. Right now...

php - preg_match string not within the href attribute

hello! i find regex kinda confusing so i got stuck with this problem: i need to insert <b> tags on certain keywords in a given text. problem is that if the keyword is within the href attribute, it would result to a broken link. the code goes like this: $text = preg_replace('/(\b'.$keyword.'\b)/i','<b>\1</b>',$text); so for cases li...

Matching a few letters and letters with numbers

I'm trying to match letters C D F H I E1 E2 CR (case insensitive) and came up with this. It'll match a single letter but wont match the E1 E2 CR. Actually it should. Whats the right way to do this? preg_match('/^([C]|[D]|[F]|[H]|[I]|[E1]|[E2]|[CR]?)$/','CR') ...

php preg_match, how to switch values

Hello guys. Please help with this little issue if you could. I would like to search a string, if a match is made, I'd like to change the value to something else. eg. if (preg_match("gmail",$email)) { // code needed to switch "gmail" for "googlemail" } This is needed because my mail server won't accept an email address in 'gmail...

Parsing text for email id's

I am trying to parse text for email id's using php / regex. Are there any classes or built in methods to do this? The text contains multiple email id's at random places. The source of the text is .doc files, which I then copy paste into forms, to be processed on submit. preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email) //from ...

preg_match: number-alphabets and commas only

Hi, How do I write a regular expression which matches number-alphabets and commas only? I came out with this one below but it doesnt work - it accepts other punctuation marks as well! # check for matches number-alphabets and commas only if(!preg_match('/([a-zA-Z0-9]|[a-zA-Z0-9\,])/', $cst_value)) { $error = true; echo '<erro...

preg_match_all doesn't reply matches array?

Hello, I have a problem with preg_match_all. While preg_match does reply the whole match as the first element of the array, preg_match_all doesn't - the first array is empty. At least with the pattern I chose (havn't tried others since it's the one I need) it doesn't work. Here is my code: preg_match_all("/<\?\?(\t| )?translate(\t| )?...

php regex - splitting strings - one output form

I'm wondering a good way of splitting strings by a | delimiter if the input strings could be of the following form: "foo, bar" "foo ,bar" "foo,bar" "foo , bar" "foo bar" "foo,,bar" So the only possible outputs strings are as: "foo|bar" "foo|bar|other|here" Independently of how many terms are within the input string. Any t...

How do I create a regular expression to capture {this} text and exclude \{that}?

Hi Guys, I need a regular expression to capture field names listed in a string. They are listed between Here are my requirements: field names in curly braces field names have no spaces curly braces can be escaped with a \ So in the following: capture {this} text and exclude \{that}? The matches are {this} but not {that}. I'm us...

PHP Preg_match: attempt to match a string where a substring does not exist in the string

Hi, I have been trying to get this regex work. Suppose to parse an URL, if the string '_skipThis' is found, don't match the string. Also backreferences are needed too. For example: String 1: a_particular_part_of_string/a/b/c/d/e/f Result: preg_match should return true Backreference: $1 -> a_particular_part_of_string, $2 -> /a/b/c/d/e/f ...

How change de eregi to preg_match in this code?

if (preg_match('^'.preg_quote($this->config_document_root), $filename)) { $AbsoluteFilename = $filename; $this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE...