Page 1 of 2

Predefined functions... explained!

Posted: Fri Jul 11, 2003 8:00 pm
by phice
I've been thinking about this for a while: let's see how challenging this forum could get by recreating the predefined functions (str_replace(), nl2br(), etc)

A few examples:

nl2br()

Code: Select all

<?
function nl2br($var)
{
$var = str_replace("\n", "<br/>", $var);
return $var;
}
?>
str_repeat() (Note: I gave the PHP community the idea to make this function ;))

Code: Select all

<?
function str_repeat($var, $times)
{
for($i = 0; $i < $times; $i++)
{
echo $var;
}
return true;
}
?>

Have fun, do what ever function you wish, and be sure to add any documentation if needed to explain the code.

Regards,
Don

Posted: Fri Jul 11, 2003 9:41 pm
by m3mn0n
:)

nice.

Posted: Sat Jul 12, 2003 6:42 am
by qartis
strrev()

Code: Select all

function strrev($string){
while (strlen($string)>0){
$array[] = substr($string,0,1);
$string = substr($string,1);
}
return implode("",array_reverse($array));
}
Though I promise you there are 15 better ways to do it :)

Posted: Sat Jul 12, 2003 7:08 am
by qartis

Code: Select all

function strrev($string){
while (strlen($string)>0){
$string2 .= substr($string,-1);
$string = substr($string,0,-1);
}
return $string2;
}
Like I said... :)

Posted: Tue Jul 22, 2003 8:45 pm
by qartis
A bit more complicated here.. :)

strtolower()

Code: Select all

<?
function strtolower($text){
for($i=0;$i<strlen($text);$i++){
$code = ord($text{$i});
$code+=(($code>=65) && ($code<=90))?32:0;
$str.=chr($code);
}
return $str;
}
?>

strtoupper()

Code: Select all

<?
function strtoupper($text){
for($i=0;$i<strlen($text);$i++){
$code = ord($text{$i});
$code-=(($code>=97) && ($code<=122))?32:0;
$str.=chr($code);
}
return $str;
}
?>

Posted: Tue Jul 22, 2003 8:58 pm
by qartis
I'm so bored...

abs()

Code: Select all

<?
function abs($num){
return ($num<0)?-$num:$num;
}
?>

Posted: Wed Jul 23, 2003 2:33 am
by Tubbietoeter
alright, the one who does preg_match or preg_replace without any help gets a cookie ... *fg*

Posted: Wed Jul 23, 2003 2:59 am
by m3mn0n
Here is one i made:

Code: Select all

<?php
function br ($x) { 
$i = 1; 
$y = $x; 
  while ($i <= $y){ 
     echo "<br>"; // or </ br> 
  $i++; 
 } 
} 
?>
It's not based on anything but it's something I wish php had built in. ;) I use it so much it's scary. heh

Posted: Wed Jul 23, 2003 3:02 am
by qartis
// or </ br>
<br />? :)

Posted: Wed Jul 23, 2003 3:04 am
by m3mn0n
That's what I meant. :oops:

it's been a long day....lol

Posted: Wed Jul 23, 2003 3:08 am
by qartis
preg_replace, eh?

Build regular expression parser in PHP
Perform a case-insensitive search via the parsed regular expression rules
Replace all instances with the new string

Yea, um, no thank you.. :)

Unless we're allowed to use similar functions for our end result (strrev()~=array_reverse()) in which case preg_replace could be done in a few minutes with awk/grep :)

Posted: Wed Jul 23, 2003 3:13 am
by m3mn0n
i'm bored so here goes another!

Code: Select all

<?php
function print ($txt)
{
  echo $txt;
}
?>
hehe

Posted: Wed Jul 23, 2003 3:34 am
by Tubbietoeter
theres no challenge though in the functions listed here, so i just wanted to see how good you guys really are ... *rofl*

Posted: Wed Jul 23, 2003 8:42 am
by pootergeist

Code: Select all

function array_pop($array_name)
	{
	$array_length = count($array_name);
	$last_value = $array_name[($array_length-1)];
	unset($array_name[($array_length-1)]);
	return $last_value;
	}

Posted: Wed Jul 23, 2003 9:10 am
by inn
here goes the built in function file_get_contents () available since php version 4.3:

Code: Select all

<?php
function file_get_contents ($fileName)
{
    return implode ("", file ($fileName));
}
?>
:D