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
https://forums.phpfreaks.com/topic/13348-limit-text/
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
https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51498
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
https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51500
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
https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51505
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
https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51507
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
https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51558
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
https://forums.phpfreaks.com/topic/13348-limit-text/#findComment-51567
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.