Jump to content

Limit amount of Smilies / Array Help


Monkuar

Recommended Posts

 $smilies=array 
    ( 
      '#(?<!\w):\)(?!\w)#i'         => '<img src=img/smileys/smile.gif alt="">', 
  '#(?<!\w):\((?!\w)#i'         => '<img src=img/smileys/sad.gif alt="">', 
  '#(?<!\w);\)(?!\w)#i'         => '<img src=img/smileys/wink.gif alt="">', 
  '#(?<!\w):cool2:(?!\w)#i'         => '<img src=img/smileys/cool2.gif alt=":cool2:">', 
   '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/huh.gif alt="">',
    '#(?<!\w)\^_\^(?!\w)#i'         => '<img src=img/smileys/happy.gif alt="">',
	 '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/ohmy.gif alt="">',
	  '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/tongue.gif alt="">',
       '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/grin.gif alt="">',
    '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/laugh.gif alt="">',
   '#(?<!\w)B\)(?!\w)#i'         => '<img src=img/smileys/cool.gif alt="B)">',
   '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/rolleyes.gif alt="">',
   '#(?<!\w)\-_\-(?!\w)#i'         => '<img src=img/smileys/sleep.gif alt="">',
   '#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/wub.gif alt="">',
   '#(?<!\w):rofl:(?!\w)#i'         => '<img src=img/smileys/rofl.gif alt=":rofl:">',
   '#(?<!\w):hail:(?!\w)#i'         => '<img src=img/smileys/hail.gif alt=":hail:">',
    '#(?<!\w):blush:(?!\w)#i'         => '<img src=img/smileys/blush.gif alt=":blush:">',
	'#(?<!\w):popcorn:(?!\w)#i'         => '<img src=img/smileys/popcorn.gif alt=":popcorn:">',
	'#(?<!\w):yahoo:(?!\w)#i'         => '<img src=img/smileys/yahoo.gif alt=":yahoo:">',
	'#(?<!\w):love:(?!\w)#i'         => '<img src=img/smileys/love2.gif alt=":love:">',
	'#(?<!\w):unknow:(?!\w)#i'         => '<img src=img/smileys/unknow.gif alt=":unknow:">',
	'#(?<!\w):drool:(?!\w)#i'         => '<img src=img/smileys/drool.gif alt=":drool:">',
	'#(?<!\w):rifle:(?!\w)#i'         => '<img src=img/smileys/rifle.gif alt=":rifle:">',
	'#(?<!\w):hi:(?!\w)#i'         => '<img src=img/smileys/hi.gif alt=":hi:">',
		'#(?<!\w):negative:(?!\w)#i'         => '<img src=img/smileys/negative.gif alt=":negative:">',
		'#(?<!\w):yoyo:(?!\w)#i'         => '<img src=img/smileys/yo-yo.gif alt=":yoyo:">',
			'#(?<!\w):throat:(?!\w)#i'         => '<img src=img/smileys/throat.gif alt=":throat:">',
			'#(?<!\w):cooldance:(?!\w)#i'         => '<img src=img/smileys/CoolDance.gif alt=":cooldance:">',
			'#(?<!\w):wallbash:(?!\w)#i'         => '<img src=img/smileys/headbash.gif alt=":wallbash:">',
			'#(?<!\w):fume:(?!\w)#i'         => '<img src=img/smileys/ranting.gif alt=":fume:">',
			'#(?<!\w):pissed:(?!\w)#i'         => '<img src=img/smileys/PissedOff.gif alt=":pissed:">',
			'#(?<!\w):evil:(?!\w)#i'         => '<img src=img/smileys/devil.gif alt=":evil:">',
				'#(?<!\w):angel:(?!\w)#i'         => '<img src=img/smileys/angel.gif alt=":angel:">',
				'#(?<!\w):finger:(?!\w)#i'         => '<img src=img/smileys/finger.gif alt=":finger:">',
				'#(?<!\w):mgun:(?!\w)#i'         => '<img src=img/smileys/machinegun.gif alt=":mgun:">',
				'#(?<!\w):banana:(?!\w)#i'         => '<img src=img/smileys/banana.gif alt=":banana:">',
				'#(?<!\w):santa:(?!\w)#i'         => '<img src=img/smileys/santa.gif alt=":santa:">',
				'#(?<!\w):heart2:(?!\w)#i'         => '<img src=img/smileys/heart2.gif alt=":heart2:">',
				'#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/cry3.gif alt="">',
				'#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/unsure.gif alt="">',
				'#(?<!\w):hug:(?!\w)#i'         => '<img src=img/smileys/hug.gif alt=":hug:">',
				'#(?<!\w):baby:(?!\w)#i'         => '<img src=img/smileys/baby.gif alt=":baby:">',
				'#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/ph34r.gif alt="">',
				'#(?<!\w):thumbsup:(?!\w)#i'         => '<img src=img/smileys/thumbsup.gif alt=":thumbsup:">',
				'#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/wacko.gif alt="">',
				'#(?<!\w):rant:(?!\w)#i'         => '<img src=img/smileys/rant2.gif alt=":rant:">',
				'#(?<!\w):praise:(?!\w)#i'         => '<img src=img/smileys/praise.gif alt=":praise:">',
				'#(?<!\w):drink:(?!\w)#i'         => '<img src=img/smileys/drinks.gif alt=":drink:">',
				'#(?<!\w)(?!\w)#i'         => '<img src=img/smileys/shrug.gif alt="">',
				'#(?<!\w):pcwhack:(?!\w)#i'         => '<img src=img/smileys/pcwhack.gif alt=":pcwhack:">',
				'#(?<!\w):gun1:(?!\w)#i'         => '<img src=img/smileys/gun1.gif alt=":gun1:">',
				'#(?<!\w):gun2:(?!\w)#i'         => '<img src=img/smileys/gun2.gif alt=":gun2:">',
				'#(?<!\w):huglove:(?!\w)#i'         => '<img src=img/smileys/huglove.gif alt=":huglove:">',
				'#(?<!\w):music:(?!\w)#i'         => '<img src=img/smileys/music.gif alt=":music:">',
					'#<3#i'         => '<img src=img/smileys/heart2.gif alt="<3">',
					'#<\_<#i'         => '<img src=img/smileys/dry.gif alt="">',
    ); 


      $text=preg_replace(array_keys($smilies), array_values($smilies), $text,7);
  foreach ($smilies as $chosen){
  $amount .= $i++;
if ($chosen >= 7){
message("You have posted to many smilies");
}
}
echo $amount;
exit;



 

 

Okay, as you can see I am limiting users to only post 7 smilies.  But this right now is only letting them post 7 of EACH smiley. So in the end, they could actually post more than well 7 of each smileys, That is NOT What I want.

 

I need your help! I need to beable to count each time they convert 1 smiley and give them a error message if they are trying to use more than 7.

 

 

Right now my $amount echos:

 

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455

 

Which is way off, it should only echo the count of smileys that were replaced?

 

 

Link to comment
Share on other sites

I think you'll need to re-read the following line, first and foremost:

$amount .= $i++;

 

Hm, I changed it to $amount = $i++;

 

And it'll echo out "55", which seems to be how many smilies I have total, problem is now I don't know how to differentiate between if a user used a preg_replace or not to count how many they used?

 

 

if (preg_replace(array_keys($smilies), array_values($smilies), $text)){
$count++;
}
echo $count;
exit;

 

I use 5- 10 smilies, still outputs only 1? Wat the hell

 

AFAIK this guy $count should be counting for me... incrementally..

Link to comment
Share on other sites

That last code will only be fired once, since it's a IF-test not a loop. So that's correct.

 

To get what you want then you have to manually loop over the content, replacing the smilies one at a time, until you've reached the maximum number of replacements you want. Either that, or use one RegExp pattern for all smilies; Something which will not work in this case.

How to manually loop over the smilies is something you'll have to play around with to find the best way. One of your options is to split the message on a valid smilie, another is to loop through it character by character. Neither of which are particularly graceful.

Link to comment
Share on other sites

That last code will only be fired once, since it's a IF-test not a loop. So that's correct.

 

To get what you want then you have to manually loop over the content, replacing the smilies one at a time, until you've reached the maximum number of replacements you want. Either that, or use one RegExp pattern for all smilies; Something which will not work in this case.

How to manually loop over the smilies is something you'll have to play around with to find the best way. One of your options is to split the message on a valid smilie, another is to loop through it character by character. Neither of which are particularly graceful.

 

Okay, i'll play.

 

 

$string = ":gun2:";
if (preg_match_all("/" . preg_quote($string) . "/i", $text, $matches)) {
if (count($matches[0]) > 10){
message("You have posted to many smilies, you are only allowed 10");
}
}

 

 

Now, Is it possible to turn this into a loop so that $string could = array_keys($smilies) ? So it checks them all? :)

 

Because there is no way in hell I am making 55 more if statements, haha.

Link to comment
Share on other sites

preg_replace will populate a variable with the number of replacements it performs, if you specify. See the fifth parameter in the manual entry. preg_replace

Dam PIKACHU, didn't even see that, wow that is awesome.

 

$text=preg_replace(array_keys($smilies), array_values($smilies), $text,7, $amount);
  
if ($amount > 10){
message("You have posted to many smilies.");
}

 

TOPIC SOLVED!!

 

Holy moly that was cake! 

 

I think I need to do more reading...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.