PHP Snippets
« 1 2 3gradient_text
Information
(PHP Version >= 3.0.6)Functions used in this snippet: str_replace, strlen, hexdec, substr, dechex, implode.
Description
str gradient_text( str str, str color1, str color2, str html )
Outputs a string as a gradient from color color1 to color color2. It outputs the colors based on the tag of type html.
Snippet
<?php
function gradient_text($str, $color1, $color2, $html='span'){
$color1 = str_replace('#', '', $color1);
$color2 = str_replace('#', '', $color2);
$color1 = strlen($color1) === 3 ? $color1{0} . $color1{0} . $color1{1} . $color1{1} . $color1{2} . $color1{2} : $color1;
$color2 = strlen($color2) === 3 ? $color2{0} . $color2{0} . $color2{1} . $color2{1} . $color2{2} . $color2{2} : $color2;
$endstr = '<' . $html . ' style="color:#' . $color1 . ';">' . $str{0} . '</' . $html . '>';
$colors[0] = hexdec(substr($color2, 0, 2));
$colors[1] = hexdec(substr($color2, 2, 2));
$colors[2] = hexdec(substr($color2, 4, 2));
$colors[3] = hexdec(substr($color1, 0, 2));
$colors[4] = hexdec(substr($color1, 2, 2));
$colors[5] = hexdec(substr($color1, 4, 2));
for($i=1; $i<strlen($str) - 1; $i++){
if($str{$i} != ' '){
$color = array();
$color[] = dechex(($colors[0] - $colors[3]) / strlen($str) * $i + $colors[3]);
$color[] = dechex(($colors[1] - $colors[4]) / strlen($str) * $i + $colors[4]);
$color[] = dechex(($colors[2] - $colors[5]) / strlen($str) * $i + $colors[5]);
$color[0] = strlen($color[0]) < 2 ? $color[0] . $color[0] : $color[0];
$color[1] = strlen($color[1]) < 2 ? $color[1] . $color[1] : $color[1];
$color[2] = strlen($color[2]) < 2 ? $color[2] . $color[2] : $color[2];
$endstr .= '<' . $html . ' style="color:#' . implode($color,'') . '">' . $str{$i} . '</' . $html . '>';
}else{
$endstr .= ' ';
}
}
$endstr .= '<' . $html . ' style="color:#' . $color2 . ';">' . $str{(strlen($str)-1)} . '</' . $html . '>';
return $endstr;
}
?>
Example
gradient_text Can be used in the following way:
<?php
echo gradient_text('red will turn into blue', '#FF0000', '#0000FF');
// out puts "red will turn into blue" as a gradient from red to blue
echo gradient_text('red will turn into blue', 'f00', '00f', 'font');
// outputs the same as above, but uses a font tag instead of span
// ( i dunno why you would do this but just in case you really want to go against good coding standards)
?>
find_version
Information
(PHP Version >= 4.3.0)Functions used in this snippet: str_replace, explode, preg_match, strstr, substr, strpos, preg_replace, file_get_contents.
Description
str find_version(str functions)
Finds the newest PHP version needed out of a list of functions. A string is returned with the newest PHP version needed.
Snippet
<?php
function find_version($functions) {
$str = str_replace(array("\r","\n",' ','(',')'),'',$functions);
$fun = explode(',',$str);
$versions = array();
foreach($fun as $key=>$value){
$str = file_get_contents('http://www.php.net/' . str_replace('_','-',$value));
preg_match('/\>\s*\((.+?)\)\<\/P/ims', $str,$matches);
$matches[0] = str_replace(array('3 >= ','4 >= ','5 >= '),'',$matches[0]);
$x = preg_replace('/[^,0-9.]/','',$matches[0]);
$x = strstr($x,',') ? substr($x,0,strpos($x,',')) : $x;
$versions[] = $x;
}
natsort($versions);
$x = end($versions);
return $x;
}
?>
Example
find_version Can be used in the following way:
<?php
echo find_version('file_get_contents,substr,in_array');
// above outputs 4.3.0
echo find_version('str_replace(),str_ireplace()');
// above outputs 5
?>
bbasic
Information
(PHP Version >= 3.0.9)Functions used in this snippet: preg_replace.
Description
str bbasic(str string)
Replaces [b],[i], and [type] with bold text, italicized text, and colored text respectively. The Functions returns an adjusted str of the supplied string.
Snippet
<?php
function bbasic($str){
$str = preg_replace('/\[b\](.+?)\[\/b\]/ims','<span class="bold">$1</span>',$str);
$str = preg_replace('/\[i\](.+?)\[\/i\]/ims','<span class="italic">$1</span>',$str);
$str = preg_replace('/\[type\](.+?)\[\/type\]/ims','<span class="type">$1</span>',$str);
return $str;
}
?>
Example
bbasic Can be used in the following way:
<?php
echo bbasic('[b]yay this is bold[b]');
// above text is turned to <span class="bold">yay this is bold</span>
echo bbasic('[type]string[/type] are [i]italic[/i]');
// above text is turned to <span class="type">string</span> are <span class="italic">italic</span>
?>
stri_replace
Information
(PHP Version >= 4.10)Functions used in this snippet: array_key_exists, count, is_array, array_keys, strtolower, str_replace, explode, strlen, substr.
Description
str stri_replace(mixed search, mixed replace, str subject)
replaces all occurances of search with replace in the subject string and returns the corrected string. It is done without regards to text case.
search and replace can bot be arrays.
Snippet
<?php
function stri_replace($word,$replace,$str){
$wordray = is_array($word) ? true : false;
$replray = is_array($replace) ? true: false;
if($wordray){
$wordkeys = array_keys($word);
$sizeword = count($wordkeys);
if($replray){
$replkeys = array_keys($replace);
$sizerepl = count($replace);
}
for($i=0; $i<$sizeword; $i++){
if($replray && array_key_exists($i,$replkeys)){
$repll = $replace[$replkeys[$i]];
}elseif($replray){
$repll = '';
}else{
$repll = $replace;
}
$str = stri_replace($word[$wordkeys[$i]],$repll,$str);
}
$ret = $str;
}else{
$str_low = strtolower($str);
$word_low = strtolower($word);
$str_low = str_replace($word_low,$word,$str_low);
$explode = explode($word,$str_low);
$count = count($explode);
$ret = '';
$holder = 0;
$len = strlen($word);
for($i=0; $i<$count; $i++){
$thislen = strlen($explode[$i]);
$ret .= substr($str,$holder,$thislen);
if($i !== ($count - 1)){
$ret .= $replace;
}
$holder = $holder + $thislen + $len;
}
}
return $ret;
}
?>
Example
stri_replace Can be used in the following way:
<?php
$str = 'I HAte Red and love Blue';
echo stri_replace('hate','despise',$str); // I despise Red and love Blue
echo stri_replace(array('hate','LOVE'), array('enjoy','despise'),$str);
// I enjoy Red and despise Blue
?>
« 1 2 3