As PHP developers, we all know the struggle of trying to remember every native PHP function.
It can be like trying to recall your high school locker combination after years of not using it. Was it strtolower() or str_lower()? And what about the difference between htmlentities() and html_entity_decode()?
It’s a problem we’ve encountered time and time again, so we’ve come up with a solution. We’ve developed a suite of functions to make our lives as PHP developers a little easier. Our functions cover various areas of PHP development, including string manipulation, working with HTML and URLs, and array manipulation.
Case fix
','> ', $str);
$str = str_replace('<',' <', $str);
$str = str_replace('(','( ', $str);
$str = str_replace(')',' )', $str);
$str = str_replace('¿','¿ ', $str);
$str = str_replace('?',' ?', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = utf8_decode($str);
$str = strtolower($str);
$str = trim($str).' ';
$arr = strtolower($str);
$str = ucwords($str);
$minus = array(
'la','el','en','to','es','un','lo','su','sus','a',
'sobre','de','los','las','suyo','su','este','y',
'ese','mi','tu','soy',
'del','de','o','u','e','con',
'a', 'an', 'the', 'and', 'but', 'or', 'for', 'nor',
'as', 'at', 'by', 'for', 'from', 'in', 'into', 'near',
'of', 'on', 'onto', 'to', 'with'
);
foreach($minus as $key => $value){
$value = strtolower($value);
$value = ucwords($value);
$value = ' '.($value).' ';
$str = str_replace(($value),strtolower($value), $str);
}
$mayus = array(
'B2B','B2C','MEV','TUT','EIA','ID','CDE','DHT','SAC',
'DTH','FV','FVD','TV','HD','KPI','CNC','1Q','2Q','EYG',
'CNV','CU','CNT','HFC','PRI','ATN','PON','OTRD','RDSI',
'SIP','PQR','CPV','ATA','STB','RTL','FVD','CPE','PRO'.
'3G','4G','ATP','TTT','PO','AI','JavaScript','JS'
);
foreach($mayus as $key => $value){
$value = strtolower($value);
$value = ucwords($value);
$value = ($value).' ';
$str = str_replace(($value),strtoupper($value), $str);
}
$str = str_replace('#',' # ', $str);
$str = str_replace('( ','(', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' )',') ', $str);
$str = str_replace(':',': ', $str);
$str = str_replace(' ,',',', $str);
$str = str_replace(' ,',',', $str);
$str = str_replace('¿ ',' ¿', $str);
$str = str_replace(' ?','? ', $str);
$str = str_replace(' :',':', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = str_replace(' ',' ', $str);
$str = trim($str);
$str = utf8_encode($str);
return $str;
}
function titleCase($str) {
return titleCaseFix($str);
}
?>
$value){
$str = str_replace(' '.$value.' ', ' ', $str);
}
}
$str = preg_replace('/\s+/', ' ', $str);
$str = trim($str);
$str=str_replace(' ',$char,$str);
return ($str);
}
function caseClear($str, $char = '-'){
return cleanCase($str, $char);
}
function caseClean($str, $char = '-'){
return cleanCase($str, $char);
}
function clearCase($str, $char = '-'){
return cleanCase($str, $char);
}
function str_clean($str, $char = '-'){
return cleanCase($str, $char);
}
function str_clear($str, $char = '-'){
return cleanCase($str, $char);
}
?>
Encode decode fix
Numbers fix
Number conversion fix
array(
"UNIT" => "TB",
"VALUE" => pow($power, 4)
),
1 => array(
"UNIT" => "GB",
"VALUE" => pow($power, 3)
),
2 => array(
"UNIT" => "MB",
"VALUE" => pow($power, 2)
),
3 => array(
"UNIT" => "KB",
"VALUE" => $power
),
4 => array(
"UNIT" => "Bytes",
"VALUE" => 1
),
);
foreach($arBytes as $arItem){
if($bytes >= $arItem["VALUE"]){
$result = $bytes / $arItem["VALUE"];
$result = str_replace(".", "." , strval(round($result, 2)))." ".$arItem["UNIT"];
break;
}
}
return $result;
}
function file_size($bytes,$power = 1024){
return fileSize($bytes,$power);
}
array(
"UNIT" => "Tons",
"VALUE" => pow($power, 3)
),
1 => array(
"UNIT" => "Kilograms",
"VALUE" => $power
),
2 => array(
"UNIT" => "Grams",
"VALUE" => 1
),
);
foreach($arGrams as $arItem){
if($grams >= $arItem["VALUE"]){
$result = $grams / $arItem["VALUE"];
$result = str_replace(".", ".", strval(round($result, 2)))." ".$arItem["UNIT"];
break;
}
}
return $result;
}
array(
"UNIT" => "L",
"VALUE" => pow($power, 3)
),
1 => array(
"UNIT" => "dL",
"VALUE" => pow($power, 2)
),
2 => array(
"UNIT" => "cL",
"VALUE" => $power
),
3 => array(
"UNIT" => "mL",
"VALUE" => 1
),
4 => array(
"UNIT" => "μL",
"VALUE" => pow(10,-3)
),
);
foreach($arMl as $arItem){
if($ml >= $arItem["VALUE"]){
$result = $ml / $arItem["VALUE"];
$result = str_replace(".", "." , strval(round($result, 2)))." ".$arItem["UNIT"];
break;
}
}
return $result;
}
?>
array(
"UNIT" => "TB",
"VALUE" => pow($power, 4)
),
1 => array(
"UNIT" => "GB",
"VALUE" => pow($power, 3)
),
2 => array(
"UNIT" => "MB",
"VALUE" => pow($power, 2)
),
3 => array(
"UNIT" => "KB",
"VALUE" => $power
),
4 => array(
"UNIT" => "Bytes",
"VALUE" => 1
),
);
foreach($arBytes as $arItem){
if($bytes >= $arItem["VALUE"]){
$result = $bytes / $arItem["VALUE"];
$result = str_replace(".", "." , strval(round($result, 2)))." ".$arItem["UNIT"];
break;
}
}
return $result;
}
function file_size($bytes,$power = 1024){
return fileSize($bytes,$power);
}
array(
"UNIT" => "Milliseconds",
"VALUE" => pow($power, -3)
),
1 => array(
"UNIT" => "Seconds",
"VALUE" => $power
),
2 => array(
"UNIT" => "Minutes",
"VALUE" => 60 * $power
),
3 => array(
"UNIT" => "Hours",
"VALUE" => 60 * 60 * $power
),
4 => array(
"UNIT" => "Days",
"VALUE" => 24 * 60 * 60 * $power
),
5 => array(
"UNIT" => "Months",
"VALUE" => 30 * 24 * 60 * 60 * $power
),
6 => array(
"UNIT" => "Years",
"VALUE" => 365 * 24 * 60 * 60 * $power
),
7 => array(
"UNIT" => "Decades",
"VALUE" => 10 * 365 * 24 * 60 * 60 * $power
),
8 => array(
"UNIT" => "Centuries",
"VALUE" => 100 * 365 * 24 * 60 * 60 * $power
),
9 => array(
"UNIT" => "Millenniums",
"VALUE" => 1000 * 365 * 24 * 60 * 60 * $power
)
);
foreach($arTime as $arItem) {
if ($time >= $arItem["VALUE"]) {
$result = $time / $arItem["VALUE"];
$result = str_replace(".", ".", strval(round($result, 2))) . " " . $arItem["UNIT"];
break;
}
}
return $result;
}
array(
"UNIT" => "m°C",
"VALUE" => pow($power, -3)
),
1 => array(
"UNIT" => "µ°C",
"VALUE" => pow($power, -6)
),
2 => array(
"UNIT" => "n°C",
"VALUE" => pow($power, -9)
),
3 => array(
"UNIT" => "p°C",
"VALUE" => pow($power, -12)
),
4 => array(
"UNIT" => "°C",
"VALUE" => 1
),
);
foreach ($arTemperature as $arItem) {
if ($temp >= $arItem["VALUE"]) {
$result = $temp / $arItem["VALUE"];
$result = str_replace(".", ".", strval(round($result, 2))) . " " . $arItem["UNIT"];
break;
}
}
return $result;
}
Sring fix
$value is the name of the array.
$ul Determines whether to create the opening and closing UL in html.
*/
function array2ul( $array = '',$id = 'list_',$keys = false,$vars = false,$title = false,$href = false ) {
$random = rand( 1000,9999 );
if( $id == 'list' ){
$id = $id.$random;
}
ob_start();
?>
$value ) {
e( ' '.$key.'="'.$value.'"' );
}
}
?>
>
if( is_array( $array ) ){
$i = 0;
foreach ( $array as $key => $value ) {
$id_ = cleanCase( $id.'_item_'.$key );
?>
-
if( is_array( $value ) ){
array_ul( $value,$id_,$keys,false,$title,$href );
}else{
$v = trim( $value );
if( $keys ){
$v = '{_n}'.$key.':{_n}{_}'.$v;
}
if( $title ){
$v = '[ '.$title.$v.' ]';
}
if( $href ){
?>
e( $v ); ?>
}else{
e( $v );
}
}
?>
$i++;
}
}else{
e( '{error-no-array}' );
}
?>
$html = ob_get_clean();
return $html;
}
function array_ul( $array = '',$id = 'list_',$keys = false,$vars = false,$title = false,$href = false ) {
return array2ul( $array,$id,$keys,$vars,$title,$href);
}
function array_to_ul( $array = '',$id = 'list_',$keys = false,$vars = false,$title = false,$href = false ) {
return array2ul( $array,$id,$keys,$vars,$title,$href);
}
?>
{text/print}: '.$title;
}
?>
i('code console'); ?> e($title ); ?>
e($title); ?>
{text/print-file} '.$bt[1]['file'].'');
e( '{text/print-line} '.$bt[1]['line'].'
');
if(is_array($array)){
e( '{text/print-entries}'.count($array).'
');
}
?>
$value) {
e('');
e(''.$key.'
');
if(is_array($value)){
array_ul($value,'print_list_',true);
}else{
e(''.$value.'
');
}
e('');
}
}else{
e('');
e($array);
e('');
}
?>
'.$print_h.'
array(
"UNIT" => "Milliseconds",
"VALUE" => pow($power, -3)
),
1 => array(
"UNIT" => "Seconds",
"VALUE" => $power
),
2 => array(
"UNIT" => "Minutes",
"VALUE" => 60 * $power
),
3 => array(
"UNIT" => "Hours",
"VALUE" => 60 * 60 * $power
),
4 => array(
"UNIT" => "Days",
"VALUE" => 24 * 60 * 60 * $power
),
5 => array(
"UNIT" => "Months",
"VALUE" => 30 * 24 * 60 * 60 * $power
),
6 => array(
"UNIT" => "Years",
"VALUE" => 365 * 24 * 60 * 60 * $power
),
7 => array(
"UNIT" => "Decades",
"VALUE" => 10 * 365 * 24 * 60 * 60 * $power
),
8 => array(
"UNIT" => "Centuries",
"VALUE" => 100 * 365 * 24 * 60 * 60 * $power
),
9 => array(
"UNIT" => "Millenniums",
"VALUE" => 1000 * 365 * 24 * 60 * 60 * $power
)
);
foreach($arTime as $arItem) {
if ($time >= $arItem["VALUE"]) {
$result = $time / $arItem["VALUE"];
$result = str_replace(".", ".", strval(round($result, 2))) . " " . $arItem["UNIT"];
break;
}
}
return $result;
}
Now, some people might argue that creating these functions is a bad practice, but we disagree. We believe that if something can increase productivity and make our lives easier, then it’s worth it. It’s always a great feeling to say, “Hey boss, I just saved us 10 minutes of coding with my super cool function!”
Of course, we understand that there are potential downsides to using our functions. If we get our hands on a project developed by a different team that never used these functions, we might have a hard time. But we believe that anything that can help us work smarter, not harder, is worth exploring.
So, let’s raise a cup of coffee (or beer, if that’s more your thing) to productivity and making our lives as developers a little easier.
Phixel is a digital design and development agency with a strong online presence on platforms such as CodePen, Dribbble, Facebook, GitHub, Behance, Instagram, LinkedIn, Medium, Twitter, Vimeo, WhatsApp, YouTube or LinkTree. By following their work and connecting with them on these platforms, you can stay up-to-date with their latest projects and developments in the digital design and development industry.