I am putting together a website that is supposed to have a large collection of free PHP scripts. If you want to, you can post any scripts you want there along with any copyright or contact information you want to go with it. I would greatly appreciate it. The website is here:

http://phpshift.fireproductions.co.uk

It gets a good amount of traffic, so if you want people to see your codes, it's a good place to put it.

    Hey, I've not seen this thread in ages. I've just been taking a look back through and saw my rather ropey PHP4 static property implementation. PHP4 does not have static class properties but it does have static function properties so we can use that instead, and the best bit is, it's painfully simple. The majority of the code is just a convaluted test.

    <?php
    
    class MyTest {
        var
            $dynamic,
            $static;
        function MyTest() {
            static $static;
            $this->static =& $static;
        }
    }
    
    $obj1 = new MyTest;
    $obj2 = new MyTest;
    
    $obj1->static = 'Blah';
    $obj1->dynamic= 'Blah';
    echo <<<END_TEXT
    \$obj1->static:  {$obj1->static}
    \$obj1->dynamic: {$obj1->dynamic}
    \$obj2->static:  {$obj2->static}
    \$obj2->dynamic: {$obj2->dynamic}
    
    
    END_TEXT;
    
    $obj2->static = 'Whoa!';
    $obj2->dynamic= 'Whoa!';
    echo <<<END_TEXT
    \$obj1->static:  {$obj1->static}
    \$obj1->dynamic: {$obj1->dynamic}
    \$obj2->static:  {$obj2->static}
    \$obj2->dynamic: {$obj2->dynamic}
    
    END_TEXT;
    
      phpshift wrote:

      It gets a good amount of traffic, so if you want people to see your codes, it's a good place to put it.

      Why not post it here? This site gets a good amount of traffic, too. Or was this just an excuse to pimp your site again?

        Weedpacket wrote:

        Why not post it here? This site gets a good amount of traffic, too. Or was this just an excuse to pimp your site again?

        Well, I'm sure these forum posts are eventually deleted. If you put it on my site, it'll be there permanently.

          No, you will be remembered forever here now. But, what's with the design on your site...ouch.

            A lot of the people here need to cool down a bit....

              Awww it reminds me of 1996, HTML to match too 😉

              I think whatever code your using to display your PHP is escaping quotes where it shouldn't too - see the password editor one.

                Well, take for example your 'Random 10-Digit Password Creator'. It screams "written by a newbie!" at me. bubblenut and myself have already given you suggestions to improve it, but these suggestions have been ignored.

                The thing is, your website lacks good content, and the poor design doesnt help to alleviate that. It is far from the 'supposed to have a large collection of free PHP scripts' goal, and doesnt look worth contributing to since what it does have isnt worth using. This is turn makes the 'gets a good amount of traffic' sound rather dubious, even if it is true.

                My suggestion is to stay around here and help out a little more. By helping out, you learn. By reading the suggestions made to help others, you improve too. You should also seek help in your existing scripts, get them improved and well formatted, and have more scripts in your current collection.

                  If a script works, why do you care how it's written?

                    If a script works, why do you care how it's written?

                    Well written (and documented) scripts are easier to understand, optimise and maintain.

                      phpstuff wrote:

                      Well, I'm sure these forum posts are eventually deleted. If you put it on my site, it'll be there permanently.

                      Well, whether this site (and hence the posts on it) will outlive yours is of course one matter. But you're right about one thing. Posts that are in violation of these boards' Acceptable Use Policy (especially those that consist of advertising) do get deleted.

                      If a script works, why do you care how it's written?

                      If you didn't care how it was written, why did you post in the Code Critique forum and ask for comments? Or was it just so that you could boost your page ranking with another link here? Your motives are in question.

                        6 months later

                        Roll an array $array[$first, ..., $mid, ..., $last] so that $mid is the first element. I.e., $array[$mid, ..., $last, $first, ...].

                        $array = array_reverse(array_merge(
                        				array_reverse(array_slice($array, 0, $mid)),
                        				array_reverse(array_slice($array, $mid))
                        		));

                        EDIT: Should have given the description. Oops. Basically, take the first $mid elements of the array and reverse them. Take the rest of the array and reverse that. Stick the two chunks back together, then reverse the whole thing. (Missy Elliot helped me with this algorithm.)

                        Rolling [a,b,c,d,e,f,g,h,i] left (did I mention it was left?) by five places:

                        [a,b,c,d,e,f,g,h,i]
                        [a,b,c,d,e][f,g,h,i]  slice
                        [e,d,c,b,a][i,h,g,f]  reverse
                        [e,d,c,b,a,i,h,g,f]   merge
                        [f,g,h,i,a,b,c,d,e]   reverse
                        

                          here is something i was just messing around with and for some reason i liked the way it look so here it is:

                          <html>
                          <head>
                          <style type="text/css">
                          
                          body 
                          {background-color:black; color:red; font-size: 15px; font-family: serif;}
                          p 
                          {background-color: white; color:black; font-family:Verdana; font-size:15px;}
                          table
                          {background-color:black; color: green; font-size:15px; font-family:Verdana;}
                          </style>
                          </head>
                          <body>
                          Hey
                          <p class="p"> hey</p>
                          <table class="table">
                          <tr><td>hey</td><td>Whats</td></tr>
                          <tr><td>up</td><td>World??</td></tr>
                          </table>
                          
                          </body>
                          </html>
                          
                            Weedpacket wrote:

                            Roll an array $array[$first, ..., $mid, ..., $last] so that $mid is the first element. I.e., $array[$mid, ..., $last, $first, ...].

                            My brain hurts now.

                            Wonderful stuff, per your usual specifications.

                            Thanks!

                              3 months later

                              A reordering of the elements of an array can be described by listing the array indices those elements end up paired with in the reordered array. For example, if the array is array('a','b','c','d','e') and the list of indices is array(3,4,2,0,1), then the reordered array is array('d','e','c','a','b').

                              For this to be effective, the listing (the permutation) needs to contain the elements 0 through to n-1 inclusive exactly once each for an n-element array. Gaps or duplicates could cause problems.

                              So what if you're given a "permutation" that might suffer in spots from exactly these flaws? How to turn it into a proper permutation?

                              Well, if there are duplicates then there is more than one possible solution (each duplicate is a tie that could be broken either way) and in the absence of further information one's as good as any other. (If one's not as good as any other then you've got more work to do getting the permutation to reflect that before you get here.)

                              So. To turn an array that is supposed to be a permutation into an array that really is a permutation, closing up any gaps and effectively tossing a coin to decide how to order the duplicates:

                              asort($permutation);
                              $permutation = array_keys($permutation);
                              asort($permutation);
                              $permutation = array_keys($permutation);
                              

                              Incidentally, if the original array is an ordinary numerically-indexed one, the first two lines produce a permutation, just not the one we want here. In fact, it's the inverse permutation, so if you ever find yourself in need of such a thing, this note supplies that, too. (The second two lines obviously therefore compute the inverse of the inverse 🙂)

                                Something I came up with not too long ago for providing a means to express the difference between two times in terms of years, months, weeks, days, hours, minutes, seconds:

                                <?php
                                /**
                                * array timeDiff(int $t1, int $t2)
                                * $t1 and $t2 must be UNIX timestamp integers, order does not matter
                                * returns array broken down into years/months/weeks/etc.
                                */
                                function timeDiff($t1, $t2)
                                {
                                   if($t1 > $t2)
                                   {
                                      $time1 = $t2;
                                      $time2 = $t1;
                                   }
                                   else
                                   {
                                      $time1 = $t1;
                                      $time2 = $t2;
                                   }
                                   $diff = array(
                                      'years' => 0,
                                      'months' => 0,
                                      'weeks' => 0,
                                      'days' => 0,
                                      'hours' => 0,
                                      'minutes' => 0,
                                      'seconds' =>0
                                   );
                                
                                   foreach(array('years','months','weeks','days','hours','minutes','seconds')
                                         as $unit)
                                   {
                                      while(TRUE)
                                      {
                                         $next = strtotime("+1 $unit", $time1);
                                         if($next < $time2)
                                         {
                                            $time1 = $next;
                                            $diff[$unit]++;
                                         }
                                         else
                                         {
                                            break;
                                         }
                                      }
                                   }
                                   return($diff);
                                }
                                
                                // sample usage:
                                $start = strtotime('2007-01-15 07:35:55');
                                $end = strtotime('2009-11-09 13:01:00');
                                $diff = timeDiff($start, $end);
                                $output = "The difference is:";
                                foreach($diff as $unit => $value)
                                {
                                   echo " $value $unit,";
                                }
                                $output = trim($output, ',');
                                echo $output;
                                ?>
                                

                                  A little tool to create SQL create statements from an XMI entity relationship diagram. Useful if you design your databases in a tool like, for example, Umbrello.

                                    2 months later

                                    So I'm flipping through one of my old programming texts (one of the ones in which I/O is principally done through punched cards) and come across an algorithm to find the intersection of two sets of numbers that is asymptotically faster than checking each element of one set to see if it appears in the other. (Remember through all this that the elements of a set are distinct, by definition; while array_intersect() will retain duplicate values, this won't.)

                                    It is limited to integers only; but since these might be ID numbers for something more interesting, that's not a huge limitation.

                                    It is noticeably faster than using array_intersect() once the sizes of the sets involved and the range of possible values climb above a couple of dozen or so. Below that, the extra overhead of having all the logic in bytecode instead of machine code dominates.

                                    This is the algorithm. To find the intersection of two sets of integers A and B:

                                    1. Multiply the values of A and B by 2

                                    2. Add one to all the values of B

                                    3. merge A and B into one list

                                    4. sort the list (this is the bit that dominates the running time)

                                    5. Walk through the sorted list; if 2n and 2n+1 are both elements of the list (if they are then, due to the sorting, they will be adjacent), then n is an element of both A and B.

                                    And here's some code to share:

                                    foreach($a as &$av) $av+=$av;
                                    foreach($b as &$bv) $bv+=$bv+1;
                                    unset($av, $bv);
                                    $merge = array_merge($a,$b);
                                    sort($merge);
                                    $n = count($merge)-1;
                                    $intersection = array();
                                    for($i=0; $i<$n; $i++)
                                    {
                                    	if(!($merge[$i]&1) && ($merge[$i+1]==$merge[$i]+1))
                                    	{
                                    		$intersection[] = $merge[$i++]>>1;
                                    	}
                                    }
                                    
                                      3 months later

                                      A potentially long chunk of text may be previewed on another page by exceprting the first n words or n characters to use as an abstract. When taking the first characters, ideally you don't want it to break off in mid-word (you don't want something containing "...she had a mandate to..." to be truncated to end with "...she had a man".)

                                      So there are two ways to specify such a truncation: counting words or counting letters.

                                      Here's a couple of functions for doing those. The first fetches the first n words from the string, while the second fetches as many words as it can short of exceeding a maximum of n characters.

                                      Some points that both functions have in common:

                                      • "Word" is defined as "any consecutive series of non-whitespace characters (that can't be made any longer without including whitespace)". The string "a sentence fragment - and another." contains six words.

                                      • Leading and trailing whitespace is trimmed from the returned string.

                                      • Apart from what is trimmed, all whitespace in the returned string is preserved unchanged from the original.

                                      • The functions really only reassemble the words; a third function does all the splitting-into-words part for both.

                                      • The functions are poorly named.

                                      function split_words($string)
                                      {
                                      	return preg_split('/(?<=\S)(?=\s)/', ltrim($string));
                                      }
                                      
                                      function truncate_words($string, $wordcount)
                                      {
                                      	return join('', array_slice(split_words($string), 0, $wordcount));
                                      }
                                      
                                      function truncate_words_to_length($string, $stringlength)
                                      {
                                      	$bits = split_words($string);
                                      	$len = $i = 0;
                                      	while($len<=$stringlength)
                                      	{
                                      		$len += strlen($bits[$i++]);
                                      	}
                                      	return rtrim(join('', array_slice($bits, 0, $i-1)));
                                      }
                                      

                                        Just because I like one-liners:

                                        function truncate_words_to_length($string, $length)
                                        {
                                           return(array_shift(explode(chr(0), wordwrap($string, $length, chr(0)))));
                                        }
                                        

                                        🙂