mrwhale Posted July 1, 2006 Share Posted July 1, 2006 ----------------------------------I SORTED THE PROBLEM OUT (plus i listed how i did it on one of the posts below ;))----------------------------------Hi all, i have a little problem. I want to limit the amount of times a piece of text from an array can be displayed.Example:[code]<?php$text = "hello this is my sentance, :hello: :hello: :hi: :blah: :hi: :hi: :hi: do de dum de";$array = array( ":hello:", ":hi:", ":blah:");// need code here that will only allow the first 4 matches in the $text to show and then make any extra ones blankecho $text;// this should echo the following: "hello this is my sentance, :hello: :hello: :hi: :blah: do de dum de"?>[/code]Thanks for any help that can be given. :) Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/ Share on other sites More sharing options...
redarrow Posted July 1, 2006 Share Posted July 1, 2006 look up substr okhttp://uk.php.net/substr Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51490 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 [quote author=redarrow link=topic=99057.msg389950#msg389950 date=1151720959]look up substr okhttp://uk.php.net/substr[/quote]Sorry, but that doesn't help me. Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51493 Share on other sites More sharing options...
ssmK Posted July 1, 2006 Share Posted July 1, 2006 yeah it does.. Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51495 Share on other sites More sharing options...
redarrow Posted July 1, 2006 Share Posted July 1, 2006 thanks ssmkcan you kindly exsplain in detail what you want to do.ps. your sites are nice well done. Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51497 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 thxFor the forum i have made, i allow people to use smileys by entering :smile: :sad: etc. into the text. I need to limit this to say 10 smileys allowed per post. Other wise the user could submit like 100000 smileys and it would seriously slow the page down.so say if the users post was this:hello everyone :smile: i am felling :sad:can u make me happy :smile: :smile: :sad: :happy: :laugh:Now what i would want to do is only allow the first 3 smilies to work, but get rid of the extra ones at the end.This is my goal, but i do not know how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51498 Share on other sites More sharing options...
redarrow Posted July 1, 2006 Share Posted July 1, 2006 <?$check1=array(":smile" ,":sad" , ":happy" ,":laugh");$check2=array(":smile" ,":sad" , ":happy" ,":laugh");$check3=array(":smile" ,":sad" , ":happy" ,":laugh");$smile_check= "$check1[0]$check2[0]$check3[0]";$sad_check= "$check1[1]$check2[1]$check3[1]";$happy_check= "$check1[2]$check2[2]$check3[2]";$laugh_check= "$check1[3]$check2[3]$check3[3]";if(!$smile_check) {echo "sorry to meny :smiles";}elseif(!$sad_check) {echo " sorry to meny :sad";}elseif(!$happy_check) {echo "sorry to meny :happys";}elseif (!$laugh_check) {echo " sorry to meny :laugh";}?> Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51500 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 Still can't get it to work. >:( Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51503 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 [quote author=redarrow link=topic=99057.msg389960#msg389960 date=1151723312]<?$check1=array(":smile" ,":sad" , ":happy" ,":laugh");$check2=array(":smile" ,":sad" , ":happy" ,":laugh");$check3=array(":smile" ,":sad" , ":happy" ,":laugh");$smile_check= "$check1[0]$check2[0]$check3[0]";$sad_check= "$check1[1]$check2[1]$check3[1]";$happy_check= "$check1[2]$check2[2]$check3[2]";$laugh_check= "$check1[3]$check2[3]$check3[3]";if(!$smile_check) {echo "sorry to meny :smiles";}elseif(!$sad_check) {echo " sorry to meny :sad";}elseif(!$happy_check) {echo "sorry to meny :happys";}elseif (!$laugh_check) {echo " sorry to meny :laugh";}?>[/quote]This isn't quite what i want. I'm not trying to limit each smiley individually, i'm trying to limit the total amount of smileys. and then remove any left over. Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51505 Share on other sites More sharing options...
redarrow Posted July 1, 2006 Share Posted July 1, 2006 <?//This code will let the user only put 3 :smiles ect ect .. in the form ok.// change what ever the form name is ok $file=$whatever.$file=":smile:smile:smile";$check1=array(":smile" ,":sad" , ":happy" ,":laugh");$check2=array(":smile" ,":sad" , ":happy" ,":laugh");$check3=array(":smile" ,":sad" , ":happy" ,":laugh");$smile_check= "$check1[0]$check2[0]$check3[0]";$sad_check= "$check1[1]$check2[1]$check3[1]";$happy_check= "$check1[2]$check2[2]$check3[2]";$laugh_check= "$check1[3]$check2[3]$check3[3]";if(eregi($smile_check,$file)) {echo "sorry to meny :smiles";}elseif(eregi($sad_check,$file)) {echo " sorry to meny :sad";}elseif(eregi($happy_check,$file)) {echo "sorry to meny :happys";}elseif(eregi($laugh_check,$file)) {echo " sorry to meny :laugh";}?> Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51507 Share on other sites More sharing options...
redarrow Posted July 1, 2006 Share Posted July 1, 2006 i wrote that code from no looking at books quite good for me.the code i provided was that the user can only enter 3 :smiles ect................what your asking is the code calledstr_replacehttp://uk.php.net/str_replaceoreregi_repalce ok.http://uk.php.net/eregi_replace Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51509 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 ok, you obviously don't understand what i mean, thx for trying to help Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51547 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 ok, i have now sorted my problem, here is how i did it[code]<?php$text = "this is a test :smile: isn't it really good :happy: today i am :testing: really :sad:";$max_smileys = 2;$array = array( ":smile:", ":happy:", ":sad:");$split_text = split( " ", $text );$x = 0;foreach( $split_text as $split ){ if( in_array( $split, $array ) ) { $x++; if( $x <= $max_smileys ) { $new .= $split . " "; } else { $new .= ""; } } else { $new .= $split . " "; }}echo $new;?>[/code]It searches through the text, keeps the specified amount of "valid" smileys, then removes any extra ones.Super happy i got it done ;D Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51558 Share on other sites More sharing options...
redarrow Posted July 1, 2006 Share Posted July 1, 2006 nice code well done. Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51560 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 [quote author=redarrow link=topic=99057.msg390023#msg390023 date=1151736167]nice code well done.[/quote]thx ;) Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51561 Share on other sites More sharing options...
.josh Posted July 1, 2006 Share Posted July 1, 2006 well damn.. i just spent a while figuring this one out. I came up with a somewhat different solution. i'll post it anyways :-\ this code assumes that you have your smilies between colons. example:hello:cuz that's how you had it in OP[code]<?phpfunction nix_smileys($text) { //find all instances of :anythinghere: and make an array of them preg_match_all("|:(.*):|U",$text, $out, PREG_SET_ORDER); $count = 0; //foreach instance of :anythinghere: foreach($out as $val) { if ($count >= 3) { //anything after the 3rd instance we will remove $newtext = preg_replace("/".$val[0]."/", '',$text); } //end if $count++; } //end foreach //return the new string return $newtext;} //end nix_smileys $text = "This :is: :an: example :string: blah :blah: more :blah: de blah";$text = nix_smileys($text);?>[/code]edited to add comments to the code Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51567 Share on other sites More sharing options...
mrwhale Posted July 1, 2006 Author Share Posted July 1, 2006 nice :) Quote Link to comment https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51576 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.