Page 1 of 1

k, i'm stumped

Posted: Tue Mar 07, 2006 9:51 am
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)

Posted: Tue Mar 07, 2006 10:11 am
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?

Posted: Tue Mar 07, 2006 10:17 am
by feyd

Posted: Tue Mar 07, 2006 10:17 am
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) ){ 
?>

Posted: Tue Mar 07, 2006 10:18 am
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.

Posted: Tue Mar 07, 2006 10:20 am
by RobertGonzalez
Sorry, I kinda saw that after I posted. Good luck.

Posted: Tue Mar 07, 2006 10:24 am
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;

}

Posted: Tue Mar 07, 2006 10:50 am
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:

Posted: Tue Mar 07, 2006 11:00 am
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 :!: