0

i have a smile function

i want a simple add on it i want to set the limit for replace codes with smiles

function smiles($shout){
    //This function searches the shout for the smilies and replaces it with the image code.
    $FileName="lists/smiles.php";
    $list = file ($FileName);
    foreach ($list as $value) {
        list ($code,$image,) = explode ("|^|", $value);
        $shout = str_replace ($code, '<img src="smiles/'.$image.'" title="'.$code.'">', $shout);
    }
    return $shout;
}

plz help

Edited by Nick Evan: Fixed formatting

2
Contributors
12
Replies
13
Views
6 Years
Discussion Span
Last Post by diafol
0

So I assume you're using smilies and want to replace all specific character combinations with smiley images.

Should be straight forward, but it does depend on your data. So what's wrong with your code?

i want a simple add on it i want to set the limit for replace codes with smiles

That doesn't make any sense.

0

this code work for me,, but i want set limit for how many smiles will be replaced<<

because some users post a lot of smiles and i want make limit for smiles per msg

0

A simple fix would be to use preg_replace:

$patterns = array(...);
$replacements = array(...);

echo preg_replace($patterns, $replacements, $string,2);

THat will allow 2 replacements for EACH smiley. Perhaps that doesn't meet you needs though.

-1

sorry but its doesn't,,,, just waiting for another help,,, thank u for your try

Votes + Comments
rude
0

i mean if user add smiles in his post like :) :( :) :( :) and the limit set to the 4 the five smile will be like this :) :( :) :( : ) > the last one not replaced because the limit set to 5

0
function strpos_arr($haystack, $input_r, $limit) {
	//following function taken from the php manual and modified (strpos)
	function strallpos($haystack,$needle_r){ 
		$result = array();
		$offset=0;
		for($i = $offset; $i<strlen($haystack); $i++){ 
			$pos = strpos($haystack, $needle_r,$i); 
			if($pos !== FALSE){ 
				$offset =  $pos; 
				if($offset >= $i){ 
					$i = $offset; 
					$result[] = $offset; 
				} 
			} 
		} 
		return $result; 
	} 
	
	$all = array(); //initialize array
	//create two separate arrays from the input
	foreach($input_r as $r){
		$smileys[] = $r[0];
		$images[] = "<img src=\"/smiles/{$r[1]}\" title=\"{$r[0]}\" />";
	}
	//build up an array of all smiley positions
	foreach($smileys as $what) {
		$add = strallpos($haystack,$what);//get values
	    $all = array_merge($all,$add);//add values to main array
    }
	
    rsort($all); //identify smileys at ordered positions
	$all = array_slice($all, -$limit); //cut down array of positions to limit
	$split_at = $all[0] + 2; //assumes all smileys ar 2 characters
	$before = substr($haystack,0,$split_at); //replacement part
	return str_replace($smileys,$images,$before) . substr($haystack,$split_at);
}

/*
USAGE
======
*/
//STRING FROM POSTED MESSAGE
$haystack = "Hello :) don't:) :( ;);):) huwyj :) jsu;) :(";

//THIS FROM THE FILE - although you could hard code this into the function
$myarray = array(
	array(":)", "img.png"),
	array(";)", "img2.png"),
	array(":(", "img3.png")

);
//SET THE NUMBER OF REPLACEMENTS
$limit = 6;

echo strpos_arr($haystack,$myarray,$limit);

Works for me, but not fully tested - maybe probs if no smileys or limit > than no smileys. As I said haven't fully tested it.

Edited by diafol: n/a

0
function strpos_arr($haystack, $input_r, $limit) {
	//following function taken from the php manual and modified (strpos)
	function strallpos($haystack,$needle_r){ 
		$result = array();
		$offset=0;
		for($i = $offset; $i<strlen($haystack); $i++){ 
			$pos = strpos($haystack, $needle_r,$i); 
			if($pos !== FALSE){ 
				$offset =  $pos; 
				if($offset >= $i){ 
					$i = $offset; 
					$result[] = $offset; 
				} 
			} 
		} 
		return $result; 
	} 
	
	$all = array(); //initialize array
	//create two separate arrays from the input
	foreach($input_r as $r){
		$smileys[] = $r[0];
		$images[] = "<img src=\"/smiles/{$r[1]}\" title=\"{$r[0]}\" />";
	}
	//build up an array of all smiley positions
	foreach($smileys as $what) {
		$add = strallpos($haystack,$what);//get values
	    $all = array_merge($all,$add);//add values to main array
    }
	
    rsort($all); //identify smileys at ordered positions
	$all = array_slice($all, -$limit); //cut down array of positions to limit
	$split_at = $all[0] + 2; //assumes all smileys ar 2 characters
	$before = substr($haystack,0,$split_at); //replacement part
	return str_replace($smileys,$images,$before) . substr($haystack,$split_at);
}

/*
USAGE
======
*/
//STRING FROM POSTED MESSAGE
$haystack = "Hello :) don't:) :( ;);):) huwyj :) jsu;) :(";

//THIS FROM THE FILE - although you could hard code this into the function
$myarray = array(
	array(":)", "img.png"),
	array(";)", "img2.png"),
	array(":(", "img3.png")

);
//SET THE NUMBER OF REPLACEMENTS
$limit = 6;

echo strpos_arr($haystack,$myarray,$limit);

Works for me, but not fully tested - maybe probs if no smileys or limit > than no smileys. As I said haven't fully tested it.

i really i don't know how to get the file content and put it in array >>> can u do that for me plz and i will be great full

0

No.
I've done the donkey work for you, I'm sure you can find out how to get data from a file into an array. Otherwise you can hard code a smiley array into the sample I gave you.

0

ok thanks a lot but i need one litil help in this code

i want to use $shout = preg_replace($code,'<img src="smiles/'.$image.'" title="'.$code.'">', $shout);
instead of str_replace but i got this error
warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash

how i can fix it?

0

OK, forget the code I offered you, if you want to use preg_replace.
$code needs to start and end with /
Try reading the php manual - you'll find it useful.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.