PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
phice
Moderator
Posts: 1416 Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:
Post
by phice » Fri Jul 11, 2003 8:00 pm
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
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Fri Jul 11, 2003 9:41 pm
nice.
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Sat Jul 12, 2003 6:42 am
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
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Sat Jul 12, 2003 7:08 am
Code: Select all
function strrev($string){
while (strlen($string)>0){
$string2 .= substr($string,-1);
$string = substr($string,0,-1);
}
return $string2;
}
Like I said...
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Tue Jul 22, 2003 8:45 pm
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;
}
?>
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Tue Jul 22, 2003 8:58 pm
I'm so bored...
abs()
Code: Select all
<?
function abs($num){
return ($num<0)?-$num:$num;
}
?>
Tubbietoeter
Forum Contributor
Posts: 149 Joined: Fri Mar 14, 2003 2:41 am
Location: Germany
Post
by Tubbietoeter » Wed Jul 23, 2003 2:33 am
alright, the one who does preg_match or preg_replace without any help gets a cookie ... *fg*
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jul 23, 2003 2:59 am
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
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Wed Jul 23, 2003 3:02 am
// or </ br>
<br />?
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jul 23, 2003 3:04 am
That's what I meant.
it's been a long day....lol
qartis
Forum Contributor
Posts: 271 Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:
Post
by qartis » Wed Jul 23, 2003 3:08 am
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
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Jul 23, 2003 3:13 am
i'm bored so here goes another!
Code: Select all
<?php
function print ($txt)
{
echo $txt;
}
?>
hehe
Tubbietoeter
Forum Contributor
Posts: 149 Joined: Fri Mar 14, 2003 2:41 am
Location: Germany
Post
by Tubbietoeter » Wed Jul 23, 2003 3:34 am
theres no challenge though in the functions listed here, so i just wanted to see how good you guys really are ... *rofl*
pootergeist
Forum Contributor
Posts: 273 Joined: Thu Feb 27, 2003 7:22 am
Location: UK
Post
by pootergeist » Wed Jul 23, 2003 8:42 am
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;
}
inn
Forum Newbie
Posts: 8 Joined: Fri Dec 13, 2002 1:21 pm
Location: Malé, Maldives
Post
by inn » Wed Jul 23, 2003 9:10 am
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));
}
?>