scriptygoddess RSS Feed
 
 
 
 

Gathered PHP snippets

Unfortunately, I'm spending the majority of my awake time actually working on PHP code, but very little of it I can actually share here, because almost none of it makes sense out of context. However, I wanted to pass along a few bits I've collected that can stand on their own, and might be useful as reference bits for other people.

So, a few variously-nifty PHP functions.

Pulling the first x words from a text string
function firstwords($str,$wordcount) {
# Gareth (omnipotent.net) 0wnz m3.
# Someday, I will be able to do this sort of thing when I grow up.
$words=preg_split('/([\s.,;]+)/',$str,$wordcount+1,PREG_SPLIT_DELIM_CAPTURE);
array_pop($words);
return(implode(",$words));
}

The issue - most existing "pull first words" functions are pretty brain-dead; they don't always take punctuation into account. This one does.

Testing a string to see if it is alphanumeric
function is_alphanumeric($test) {
return (preg_match("/^[a-z0-9 ]+$/i", $test));
}

Testing a string to see if it is a valid email address
function valid_email($email) {
# thanks again, Gareth - omnipotent.net
$host = substr($weenie,strpos($weenie,"@") + 1);
if (eregi("^([[:alnum:]_%+=.-]+)@([[:alnum:]_.-]+)\.([a-z]{2,3}|[0-9]{1,3})$",$weenie)
&& (checkdnsrr($host,"MX") || checkdnsrr($host,"A"))) {
return 1;
} else {
return 0;
}
}

Most people forget to test things like the wacky compuserve emails. My lovely sysadmin, Gareth, gave me this function. It checks that, as well as checking the machine name for validity. Obsessive? Only a little… But extremely useful nonetheless.

Random password generator

I rather like the one available at befriend.com and have been using it on quarto.

The rest really doesn't make sense out of context…but let's just say that SQL and PHP and I have been getting mighty, mighty cozy lately. I feel pretty bad about not being able to contribute code here, so if you have questions in those realms…ask! Please! I'd feel better! :)

8 Responses to “Gathered PHP snippets”

  1. 1
    Jennifer:

    THANK YOU, Amy!
    I actually do have a question… It's in the comments section on that last post regarding the special characters function…

  2. 2
    oscar:

    5{ generator you say I think has too many code, try this:

    <?php

    $pass_long = 15;

    srand( ( double ) microtime() * 1000000 );
    $unique = md5( rand( 0, 9999999 ) );

    print 'password: '.substr( $unique, 0, $pass_long )."\n";

    ?>

    Hope you like it!

  3. 3
    Amy:

    Well, I agree - that's shorter, but I still wouldn't use it.

    Here's why:

    oO0 (lowercase, uppercase, zero)
    il1 (lowercase, lowercase, numeral)

    All valid options for an alphanumeric password, but virtually impossible to distinguish in some fonts. I greatly appreciate passwords that don't require me to figure out what a character is, and I like being able to specify if numbers or letters may be repeated or not.

  4. 4
    Jennifer:

    Just tried the validate email thing… and it didn't like my email address (probably something I'm doing wrong)… haven't spent a lot of time testing it, but just so I have it here for later… I used this and it worked:

    ereg ("^.+@.+\\..+$",$email)

    will resolve to true if $email is valid…

  5. 5
    photomatt:

    Your validate email function doesn't work with 4 letter domain extensions like .info.

  6. 6
    Amy:

    Ugh, I bet you're right.

    The nasty part of it: we could increase that to four digits, except for that wretched .museum TLD. Ye gods, I should hunt down and personally hurt the people who approved that stupid TLD.

    I will ponder switching it to allowing seven characters after the final '.' or moving to some equally-awkward but fully functional version like what's available on this PHP help page (search for the comment by mail(at)philipp-louis.de).

    Even looking at THAT function makes me want to cry. Ugh.

  7. 7
    Amy:

    Ok, yes. Screw that really long version.

    Change {2,3} to {2,6} and that should cover everything up to .museum….and bring me my flamethrower for those who chose such a brain-dead TLD. ;)

  8. 8
    gnif:

    your email validation code is broken, you reference the var $weenie when it should be $email, as follows:

    function valid_email($email) {
    # thanks again, Gareth - omnipotent.net
    $host = substr($weenie,strpos($weenie,"@") + 1);
    if (eregi("^([[:alnum:]_%+=.-]+)@([[:alnum:]_.-]+)\.([a-z]{2,3}|[0-9]{1,3})$",$weenie)
    && (checkdnsrr($host,"MX") || checkdnsrr($host,"A"))) {
    return 1;
    } else {
    return 0;
    }
    }

Bookmarks

WordPress Resources

Meta