scriptygoddess

17 Sep, 2002

Gathered PHP snippets

Posted by: amy In: Scripts

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 | Jennifer

September 17th, 2002 at 10:47 am

Avatar

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 | oscar

September 17th, 2002 at 11:41 am

Avatar

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 | Amy

September 17th, 2002 at 12:33 pm

Avatar

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 | Jennifer

September 17th, 2002 at 7:15 pm

Avatar

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 | photomatt

September 17th, 2002 at 11:57 pm

Avatar

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

6 | Amy

September 18th, 2002 at 12:17 am

Avatar

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 | Amy

September 18th, 2002 at 11:49 am

Avatar

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 | gnif

April 4th, 2004 at 7:10 am

Avatar

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;
}
}

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements