Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Competition (have fun and show us your stuff)
Old 04-19-2011, 09:59 PM PHP Competition (have fun and show us your stuff)
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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
  
}
  
?>
mgraphic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-19-2011, 10:01 PM strlen()
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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($string0) !== false)
        {
            
$count++;
            
            
$string substr($string1);
        }
    }
    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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-19-2011, 10:21 PM Re: PHP Competition (have fun and show us your stuff)
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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 0true$i++)
    {
        if(!isset(
$string[$i]))
            return 
$i;
    }

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-19-2011, 10:26 PM Re: PHP Competition (have fun and show us your stuff)
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by NullPointer View Post
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 0true$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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-29-2011, 07:07 AM Re: PHP Competition (have fun and show us your stuff)
Junior Talker

Posts: 1
Trades: 0
$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));
dawsba is offline
Reply With Quote
View Public Profile
 
Old 05-01-2011, 01:36 PM Re: PHP Competition (have fun and show us your stuff)
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by dawsba View Post
$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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-02-2011, 04:36 AM ltrim()
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 05-05-2011, 06:08 PM Re: ltrim()
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by lizciz View Post
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Competition (have fun and show us your stuff)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 2.49772 seconds with 12 queries