PDA

View Full Version : Limit text


mrwhale
07-01-2006, 02:10 AM
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. :)

fci
07-01-2006, 03:44 AM
here is a cool method

$your_arr = array(':helo');
$my_stuff = "blahgadf :helo :helo";
foreach ($your_arr as $arr) {

$my_stuff = preg_replace("/(.*$arr.*)(?:$arr)?(.*)/m", "\1\2", $my_stuff);

}

if that doesn't do it someone else can figure out, pass me a beer!!

mrwhale
07-01-2006, 04:47 AM
here is a cool method

$your_arr = array(':helo');
$my_stuff = "blahgadf :helo :helo";
foreach ($your_arr as $arr) {

$my_stuff = preg_replace("/(.*$arr.*)(?:$arr)?(.*)/m", "\1\2", $my_stuff);

}

if that doesn't do it someone else can figure out, pass me a beer!!


Thats not what i'm looking for. Thanks anyway...


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.

fci
07-01-2006, 04:54 AM
in vbulletin it will say the following if you try to post too manu smilies:
You have included 330 images in your message. You are limited to using 10 images so please go back and correct the problem and then continue again.

Images include use of smilies, the vB code [img] tag and HTML <img> tags. The use of these is all subject to them being enabled by the administrator.

that seems a more ideal method to deal with it because then you can just count the usage of the smilies instead and you're not modifying user input then, pass me a beer?

mrwhale
07-01-2006, 07:43 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

fci
07-01-2006, 08:18 AM
I am happy you figured it out on your own and hopefully learned something in the process...

mrwhale
07-01-2006, 08:54 AM
I am happy you figured it out on your own and hopefully learned something in the process...

yeah, learnt a new function in_array()