PHP Competition (have fun and show us your stuff)
04-19-2011, 09:59 PM
|
PHP Competition (have fun and show us your stuff)
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
What would a built-in PHP function look like if itself were scripted in PHP?
Try your skill in trying to re-create a PHP function of your choice! Learn from others as well as learning from your peers.
THE RULES:
1. You may choose any PHP built-in function or class that exists in the current distributed build of PHP.
2. You cannot use the original function or method of the one you are cloning, but you may have a recurcive call to the cloned function or method itself.
3. Your function must behave excatly as the way the PHP documentation describes the original function. Extra points if you can trigger errors just like the documentation explains as well.
COMMUNITY FEEDBACK:
1. If you find a conflict in somebody's submission that does not adhere to the original PHP documentation, you are encouraged to share.
2. If you have more effecient ideas of somebody's submission, you are encouraged to share.
3. When you comment on somebody's submission, reply directly from the source post.
SUBMISSION FORMAT:
Please use the following guidelines when submitting a new cloned function:
Title you post of the name of the original PHP function or class name.
function_name()
Link to PHP documentation...
Code:
Return value type, function name, argument types as listed in the PHP documentation
Description given from the PHP documentation
PHP Code:
<?php // Example PHP code - Prepend the function name with PHP_ function PHP_function_name(){ // implimentation code } ?>
|
|
|
|
04-19-2011, 10:01 PM
|
strlen()
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
strlen()
http://us.php.net/manual/en/function.strlen.php
Code:
int strlen ( string $string )
Returns the length of the given string.
PHP Code:
function PHP_strlen($string) { $count = 0; if (is_scalar($string)) { $string = (string)$string; while (substr($string, 0) !== false) { $count++; $string = substr($string, 1); } } else { trigger_error('PHP_strlen() expects parameter 1 to be string, ' . gettype($string) . ' given', E_USER_WARNING); return NULL; } return $count; }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
04-19-2011, 10:21 PM
|
Re: PHP Competition (have fun and show us your stuff)
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
If you treat the string like an array it becomes much simpler:
PHP Code:
function PHP_strlen($string)
{
if(!is_scalar($string))
{
trigger_error('PHP_strlen() expects parameter 1 to be string, ' . gettype($string) . ' given', E_USER_WARNING);
return NULL;
}
$string = (string) $string;
for($i = 0; true; $i++)
{
if(!isset($string[$i]))
return $i;
}
}
|
|
|
|
04-19-2011, 10:26 PM
|
Re: PHP Competition (have fun and show us your stuff)
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Quote:
Originally Posted by NullPointer
If you treat the string like an array it becomes much simpler:
PHP Code:
function PHP_strlen($string) { if(!is_scalar($string)) { trigger_error('PHP_strlen() expects parameter 1 to be string, ' . gettype($string) . ' given', E_USER_WARNING); return NULL; } $string = (string) $string; for($i = 0; true; $i++) { if(!isset($string[$i])) return $i; } }
|
Yes, I agree but it was the only thing I could think of at the time without using the original strlen function, but I think your advice should do the trick!
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
04-29-2011, 07:07 AM
|
Re: PHP Competition (have fun and show us your stuff)
|
Posts: 1
|
$s1 = 'Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit';
function PHP_str_split($string=NULL)
{
if(is_string($string)&&strlen($string)>0)
{
$length = strlen($string);
for($i=0;$i<=$length-1;$i++)
{
$str_array[$i]=substr($string,$i,1);
}
return $str_array;
}
else
{
trigger_error('PHP_str_split() expects parameter 1 to be string, a '.gettype($string).' given', E_USER_WARNING);
return NULL;
}
}
var_dump(PHP_str_split($s1));
|
|
|
|
05-01-2011, 01:36 PM
|
Re: PHP Competition (have fun and show us your stuff)
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Quote:
Originally Posted by dawsba
$s1 = 'Lorem_ipsum_dolor_sit amet,_consectetur_adipisicing_elit';
function PHP_str_split($string=NULL)
{
if(is_string($string)&&strlen($string)>0)
{
$length = strlen($string);
for($i=0;$i<=$length-1;$i++)
{
$str_array[$i]=substr($string,$i,1);
}
return $str_array;
}
else
{
trigger_error('PHP_str_split() expects parameter 1 to be string, a '.gettype($string).' given', E_USER_WARNING);
return NULL;
}
}
var_dump(PHP_str_split($s1));
|
Good one, I added the optional second parameter string length
PHP Code:
function PHP_str_split($string, $split_length = 1) { if (!is_scalar($string)) { trigger_error('PHP_str_split() expects parameter 1 to be string, a '.gettype($string).' given', E_USER_WARNING); return false; } else if ($split_length < 1) { return false; } $string = (string)$string; $split_length = (int)$split_length; $str_array = array(); for ($i = 0; $i < strlen($string); $i += $split_length) { $str_array[] = substr($string, $i, $split_length); } return $str_array; }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
05-02-2011, 04:36 AM
|
ltrim()
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
string ltrim ( string $str [, string $charlist ] )
http://php.net/manual/en/function.ltrim.php
Wasn't sure how to implement the range feature (..) in the optional charList paramater. Feel free to upgrade.
PHP Code:
function PHP_ltrim( $str, $charList = "" ) {
if (!is_string($str)) { trigger_error('PHP_ltrim() expects parameter 1 to be string, '.gettype($str).' given', E_USER_WARNING); } if (!is_string($charList)) { trigger_error('PHP_ltrim() expects parameter 2 to be string, '.gettype($charList).' given', E_USER_WARNING); }
if (charList == "") { $strip = new array(" ", "\t", "\n", "\r", "\0", "\x0B"); } else { $strip = str_split($charList); }
$con = TRUE; $pos = 0; while($con) { if (in_array($str[$pos], $strip) { $pos++; } else { $con = FALSE; } }
return substr($str, $pos);
}
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 05-02-2011 at 04:40 AM..
Reason: Added some error handling
|
|
|
|
05-05-2011, 06:08 PM
|
Re: ltrim()
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Quote:
Originally Posted by lizciz
string ltrim ( string $str [, string $charlist ] )
http://php.net/manual/en/function.ltrim.php
Wasn't sure how to implement the range feature (..) in the optional charList paramater. Feel free to upgrade.
PHP Code:
function PHP_ltrim( $str, $charList = "" ) { if (!is_string($str)) { trigger_error('PHP_ltrim() expects parameter 1 to be string, '.gettype($str).' given', E_USER_WARNING); } if (!is_string($charList)) { trigger_error('PHP_ltrim() expects parameter 2 to be string, '.gettype($charList).' given', E_USER_WARNING); } if (charList == "") { $strip = new array(" ", "\t", "\n", "\r", "\0", "\x0B"); } else { $strip = str_split($charList); } $con = TRUE; $pos = 0; while($con) { if (in_array($str[$pos], $strip) { $pos++; } else { $con = FALSE; } } return substr($str, $pos); }
|
You can probably use range(). Just split the string by .. and if there are more than one element, use range($array[0], $array[1])
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
|
« Reply to PHP Competition (have fun and show us your stuff)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|