Jump to content

Limit text


mrwhale

Recommended Posts

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

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 blank

echo $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. :)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

<?

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

}
?>

Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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

}
?>
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
<?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);
?>
[/code]
edited to add comments to the code
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.