[PHP] preg_match_all problem
Hi, Below is my code for a templating kind of script, it basically searches for 'tags' like this {movies 30} and replaces them, the problem is that if there are 2 tags (eg: {movies 30}{movies 60}) it only replaces the first one...so I tried using a preg_match_all... I tried using a preg_match_all at: preg_match($pattern,$content,$matches[]); but then $res is blank when $res usually contains the tag names... (using a print_r($res); I just get Array ( [0] = Array ) ) **Start of code* $categories=array(movies,action,cartoons,textlinks); // This gets the tags and put it into matches[] foreach ($categories as $value) { $pattern = /{.$value. ([0-9_]+)?}/; preg_match($pattern,$content,$matches[]); } foreach ($matches as $key =$value){ preg_match(/\w+/,$value[0],$res); //print_r($res); ***End of code* What am I doing wrong? Thanks, Ryan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match_all problem
From: Ryan A [EMAIL PROTECTED] Below is my code for a templating kind of script, it basically searches for 'tags' like this {movies 30} and replaces them, the problem is that if there are 2 tags (eg: {movies 30}{movies 60}) it only replaces the first one...so I tried using a preg_match_all... I tried using a preg_match_all at: preg_match($pattern,$content,$matches[]); but then $res is blank when $res usually contains the tag names... (using a print_r($res); I just get Array ( [0] = Array ) ) **Start of code* $categories=array(movies,action,cartoons,textlinks); // This gets the tags and put it into matches[] foreach ($categories as $value) { $pattern = /{.$value. ([0-9_]+)?}/; preg_match($pattern,$content,$matches[]); } foreach ($matches as $key =$value){ preg_match(/\w+/,$value[0],$res); //print_r($res); ***End of code* What am I doing wrong? I see where things are being matched, but how are they being replaced? What you really need here is preg_replace_callback(), probably. ?php $result = preg_replace_callback('/\{([a-z]+) ([0-9]+)\}/i','callback',$yourtext); function callback($matches) { //$matches[1] is movies, cartoons, textlinks, etc //$matches[2] is the number that was matched //this function should return the string to replace //{movies 20} {cartoon 10}, etc... } ? ---John Holmes... UCCASS - PHP Survey System http://www.bigredspark.com/survey.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: [PHP] preg_match_all problem
From: Ryan A [EMAIL PROTECTED] $content = str_replace($matches[$index][0],$value,$content); If you are interested in seeing the whole script, just scroll down. I'm not... but thanks. Did you read what I wrote? using preg_match twice and then str_replace is a waste. Learn to use preg_replace_callback() to make this worthwhile. ---John Holmes... UCCASS - PHP Survey System http://www.bigredspark.com/survey.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Preg_match_all problem
Can anyone see why I'm getting this error message: Warning: No ending delimiter '^' found in C:\Inetpub\wwwMIS_DEV\import_mvs.inc.php on line 126 From this line: if(preg_match_all(^([00])?.*$,$called,$found)) { substring($called,2,9); } I basically have a phone bill that sometimes has the phone number preceeded by 00 and sometimes not. When it has the 00 I want to trim it with the substring command. Thanks, Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_match_all problem
Jeff McKeon wrote: Can anyone see why I'm getting this error message: Warning: No ending delimiter '^' found in C:\Inetpub\wwwMIS_DEV\import_mvs.inc.php on line 126 From this line: if(preg_match_all(^([00])?.*$,$called,$found)) { substring($called,2,9); } I basically have a phone bill that sometimes has the phone number preceeded by 00 and sometimes not. When it has the 00 I want to trim it with the substring command. Thanks, Jeff You need to enclose your regex in /regex/ if ( preg_match_all ( /^([00])?.*$/, $called, $found ) ) -- *** * _ __ __ __ _ * John Nichel * * | |/ /___ __ \ \/ /__ _ _| |__ ___ __ ___ _ __ * 716.856.9675 * * | ' / -_) _` \ \/\/ / _ \ '_| / /(_-_/ _/ _ \ ' \ * 737 Main St. * * |_|\_\___\__, |\_/\_/\___/_| |_\_\/__(_)__\___/_|_|_|* Suite #150 * * |___/ * Buffalo, NY * * http://www.KegWorks.com[EMAIL PROTECTED] * 14203 - 1321 * *** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_match_all problem
From: Jeff McKeon [EMAIL PROTECTED] Can anyone see why I'm getting this error message: Warning: No ending delimiter '^' found in C:\Inetpub\wwwMIS_DEV\import_mvs.inc.php on line 126 From this line: if(preg_match_all(^([00])?.*$,$called,$found)) Because you do not have an ending delimiter. In fact, you don't have a starting delimiter, either, but preg assumes you meant the ^ character to be your delimiter. This is explained in all of the examples in the manual, but you must surround your regular expression by a delimiter such as the / or # character. This is because you can include modifiers after the delimiters to further create your regex. if(preg_match_all(/^([00])?.*$,$called,$found)) Although I doubt that'll work like you want it since [00] matches a zero, or another zero. It only matches one character. I basically have a phone bill that sometimes has the phone number preceeded by 00 and sometimes not. When it has the 00 I want to trim it with the substring command. I wouldn't even use preg for this. Just use ltrim() or if(substr($called,0,2)=='00')... ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_match_all problem
From: John W. Holmes [EMAIL PROTECTED] From: Jeff McKeon [EMAIL PROTECTED] Can anyone see why I'm getting this error message: Warning: No ending delimiter '^' found in C:\Inetpub\wwwMIS_DEV\import_mvs.inc.php on line 126 From this line: if(preg_match_all(^([00])?.*$,$called,$found)) Because you do not have an ending delimiter. In fact, you don't have a starting delimiter, either, but preg assumes you meant the ^ character to be your delimiter. This is explained in all of the examples in the manual, but you must surround your regular expression by a delimiter such as the / or # character. This is because you can include modifiers after the delimiters to further create your regex. if(preg_match_all(/^([00])?.*$,$called,$found)) damn... i left out the ending delimiter, too.. :) if(preg_match_all(/^([00])?.*$/,$called,$found)) ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_match_all problem
John W. Holmes wrote: damn... i left out the ending delimiter, too.. :) Chalk one up to Monday. ;) -- *** * _ __ __ __ _ * John Nichel * * | |/ /___ __ \ \/ /__ _ _| |__ ___ __ ___ _ __ * 716.856.9675 * * | ' / -_) _` \ \/\/ / _ \ '_| / /(_-_/ _/ _ \ ' \ * 737 Main St. * * |_|\_\___\__, |\_/\_/\___/_| |_\_\/__(_)__\___/_|_|_|* Suite #150 * * |___/ * Buffalo, NY * * http://www.KegWorks.com[EMAIL PROTECTED] * 14203 - 1321 * *** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_match_all problem
Hi, I'm a frog (french), I,ve some troubles with the preg_match_all function. input type=TEXT name=search maxlength=50 size=36 $strSearchWords = Trim($search); $pattern = $strSearchWords; andwhile (!feof($openFile)) { $strFileContents .= fgets($openFile, 4096); } $strPageTitle = htmlentities(GetFileMetaTag(title, /title, $strFileContents)); $strPageDescription = htmlentities(GetFileMetaTag(meta name=\description\ content=\, \, $strFileContents)); $strPageKeywords = htmlentities(GetFileMetaTag(meta name=\keywords\ content=\, \, $strFileContents)); $strFileContents = $strPageTitle. .$strPageDescription. .$strPageKeywords; $strFileContents = str_replace(; , ,$strFileContents); $strFileContents = str_replace(,, ,$strFileContents); then I get the following warning message: Delimiter must not be alphanumeric or backslash in : if (preg_match_all($pattern,$strFileContents,$match) 0) WHAT CAN I DO ??? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_match_all problem
Hi, I'm a frog (french), I,ve some troubles with the preg_match_all function. input type=TEXT name=search maxlength=50 size=36 $strSearchWords = Trim($search); $pattern = $strSearchWords; andwhile (!feof($openFile)) { $strFileContents .= fgets($openFile, 4096); } $strPageTitle = htmlentities(GetFileMetaTag(title, /title, $strFileContents)); $strPageDescription = htmlentities(GetFileMetaTag(meta name=\description\ content=\, \, $strFileContents)); $strPageKeywords = htmlentities(GetFileMetaTag(meta name=\keywords\ content=\, \, $strFileContents)); $strFileContents = $strPageTitle. .$strPageDescription. .$strPageKeywords; $strFileContents = str_replace(; , ,$strFileContents); $strFileContents = str_replace(,, ,$strFileContents); then I get the following warning message: Delimiter must not be alphanumeric or backslash in : if (preg_match_all($pattern,$strFileContents,$match) 0) WHAT CAN I DO ??? I had this problem only a day ago. You have forgotten to add slashes to your pattern. The variable $pattern looks something like this: \d+\s when it should look like this: /\d+\s/ Hope that was helpful, Archie --- Archibald Zimonyi [EMAIL PROTECTED] There is no logic like no logic -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php