k, i'm stumped

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

k, i'm stumped

Post by s.dot »

I'm going to try to simplify my problem, so i hope it's not the complexity of it that's causing the errors.. if it is, i'll post my exact code down the line, but i'll try to recreate it in a simple manner first

I have a $_POST['text'] that I'm going to be processing.


I also have an array

Code: Select all

$array = array('a','b','c','d','e','f');
I'm going to be searching $_POST['text'] for matches in the array followed by a number.

IE: I'd want to find

a#1
b#3
f#7

etc.

so I figured something like this would work

Code: Select all

foreach($array AS $code){
   if(preg_match("/$code#(\d{1,2})/",$text,$matches){
      // replace it with some stuff
   }
}
But I keep getting a bunch of errors like this:

Code: Select all

Warning: preg_match_all(): Compilation failed: unmatched parentheses at offset 2 in /home/smptest/public_html/class/text.class.php on line 233

Warning: preg_match_all(): Compilation failed: unmatched parentheses at offset 2 in /home/smptest/public_html/class/text.class.php on line 233

Warning: preg_match_all(): Compilation failed: unmatched parentheses at offset 1 in /home/smptest/public_html/class/text.class.php on line 233

Warning: preg_match_all(): Compilation failed: unmatched parentheses at offset 1 in /home/smptest/public_html/class/text.class.php on line 233

Warning: preg_match_all(): Compilation failed: missing ) at offset 9 in /home/smptest/public_html/class/text.class.php on line 233

Warning: preg_match_all(): Compilation failed: missing ) at offset 8 in /home/smptest/public_html/class/text.class.php on line 233
Is there something wrong in that general code, or would it be more code specific (in which case I'll post the exact, more complex, code)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yeah, no clue why this was moved... this isn't a regex problem.

anyways, I figured out why it's telling me unmatched parenthesis

some of my array elements look like array(':)',':('); since they are emoticons text
shouldn't it be reading the ) as a string and not as a parenthesis?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You didn't close your "IF" paranthesis...

Code: Select all

<?php
if(preg_match("/$code#(\d{1,2})/",$text,$matches){ 
?>
Change it to this and it should work

Code: Select all

<?php
if( preg_match("/$code#(\d{1,2})/",$text,$matches) ){ 
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Everah wrote:You didn't close your "IF" paranthesis...

Code: Select all

<?php
if(preg_match("/$code#(\d{1,2})/",$text,$matches){ 
?>
Change it to this and it should work

Code: Select all

<?php
if( preg_match("/$code#(\d{1,2})/",$text,$matches) ){ 
?>
that was just a typo in my psuedo-code, but good eye :P

I'll try your suggestion, feyd.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sorry, I kinda saw that after I posted. Good luck.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

preg_quote works like a charm.

but is there an easier (less intensive) way of doing this function?

Code: Select all

function replaceSpecialSmileyText($text){
	
	#$this->getSmileyText2 returns an array of emoticons .. IE : - ) : - (  etc
	foreach($this->getSmileyText2() AS $code){

		$code = preg_quote($code,'/');
		if(preg_match_all("#$code\#(\d{1,2})#im",$text,$matches)){
			
			foreach($matches[0] AS $smiley){
				$text = str_replace($smiley," bob ",$text);
			}
		}
	}

	return $text;

}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

function replaceSpecialSmileyText($text){
	
	#$this->getSmileyText2 returns an array of emoticons .. IE : - ) : - (  etc
        $code = implode('|',array_map(createfunction('$a','return preg_quote($a,\'#\');'),array_values($this->getSmileyText2()));
	if(preg_match_all("#(?:$code)\#(\d{1,2})#im",$text,$matches)){
		foreach($matches[0] AS $smiley){
			$text = str_replace($smiley," bob ",$text);
		}
	}

	return $text;

}
:idea:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

well that was what I had so far of the function.. had I known you were going to do that i would've waited till the finale of it to ask :P

it ended up looking like this

Code: Select all

function replaceSpecialSmileyText($text){
		
	foreach($this->getSmileyText2() AS $key => $code){
			
		$code = preg_quote($code,'/');
			
		if(preg_match_all("#$code\#(\d{1,2})#im",$text,$matches)){
			$i=0;
			foreach($matches[0] AS $smiley){
				$simg = $this->getSmileyIMG($matches[1][$i]);
				$text = str_replace($smiley,$simg[$key],$text);
				$i++;
			}
			
		}
		
	}
		
	return $text;
	
}
I'll try to incorporate your above function so it'll work similar to that code.

Thanks a lot. I bow to your knowledge :!:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply