Jump to content


Photo

Limit text


  • Please log in to reply
16 replies to this topic

#1 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 12:47 AM

----------------------------------

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:

<?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 blank

echo $text;

// this should echo the following: "hello this is my sentance, :hello: :hello: :hi: :blah: do de dum de"

?>

Thanks for any help that can be given. :)

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 July 2006 - 02:29 AM

look up substr ok



http://uk.php.net/substr

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 02:40 AM

look up substr ok



http://uk.php.net/substr


Sorry, but that doesn't help me.

#4 ssmK

ssmK
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 01 July 2006 - 02:42 AM

yeah it does..

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 July 2006 - 02:50 AM

thanks ssmk

can you kindly exsplain in detail what you want to do.

ps. your sites are nice well done.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 02:57 AM

thx


For 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.

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 July 2006 - 03:08 AM

<?

$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";

}
?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 03:40 AM

Still can't get it to work.  >:(

#9 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 03:45 AM

<?

$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";

}
?>



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.

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 July 2006 - 04:00 AM

<?
//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";

}
?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 July 2006 - 04:08 AM

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 called

str_replace
http://uk.php.net/str_replace
or
eregi_repalce ok.
http://uk.php.net/eregi_replace
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 06:07 AM

ok, you obviously don't understand what i mean, thx for trying to help

#13 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 06:39 AM

ok, i have now sorted my problem, here is how i did it

<?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;

?>

It searches through the text, keeps the specified amount of "valid" smileys, then removes any extra ones.

Super happy i got it done ;D

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 01 July 2006 - 06:42 AM

nice code well done.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 06:45 AM

nice code well done.


thx ;)

#16 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 July 2006 - 07:15 AM

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

<?php
function 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);
?>
edited to add comments to the code
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#17 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 July 2006 - 08:03 AM

nice :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users