Your browser (Internet Explorer 7 or lower) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.


Navigate / search

24 Handy PHP One-Liners for Strings

Here are 24 handy PHP one-liners you can use to check and manipulate strings. Do you know all these?

// #1: add $n leading zeroes to a number
$str = str_pad($number, $n, "0", STR_PAD_LEFT); 

// #2: find similarity between two strings and return $percentage 
similar_text($str1, $str2, $percentage); 

// #3: prepend http:// to URL 
if (!preg_match("/^(http|https|ftp):/", $url)) { $url = 'http://'.$url; }  

// #4: remove duplicate characters from a string
$str = preg_replace('#(_)+#','_',$str); 

// #5: clean a string for filename
$str = preg_replace("/[^a-z0-9-]/", "-", strtolower($str)); 

// #6: cut of $text after $num number of words
$text = implode(" ",array_slice(explode(" ", $text), 0, $num));

// #7: get $num paragraphs of random Lorem Ipsum text
$text = simplexml_load_file(''.$num.'&what=paras&start=0')->lipsum;

// #8: remove empty lines
$text = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $text);

// #9: shuffle letters in a string
$shuffled = str_shuffle($str);

// #10: get last character of a string
$lastchar = substr($str, -1);

// #11: check if $str ends with $end
if (strpos($str, $end, strlen($str) - strlen($end))) { echo "String ends with ".$end; }

// #12: check valid URL
if (filter_var($url, FILTER_VALIDATE_URL)) { echo "URL is valid"; }

// #13: check valid email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo "Email is valid"; }

// #14: get a short URL
$short_url = file_get_contents("".$url);

// #15 get a unique string
$str = uniqid(); // uses server time in microseconds

// #16: check if multiple variables are set
if (isset($a, $b, $c)) { echo "Yes, all three variables are set"; }

// #17: returns the number of characters that are different
echo levenshtein('tothbrush', 'toothbrush'); // outputs 1 

// #18: compare how words are pronounced by returning the sound key
echo soundex('stake')." sounds like ".soundex('steak'); // (key = S320)

// #19: get different parts of a url
$url_segments = parse_url($url);

// #20: get meta data if a webpage
$meta = get_meta_tags('');

// #21: remove URLs from string
$str = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $str);

// #22: output number as currency string (use setlocale/LC_MONETARY to set currency)
echo money_format('%i', $number);

// #23: output HTML code by replacing < and >
echo strtr($html,array('<'=>'<', '>'=>'>')); 

// #24: get file extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);

Walt Disney World Transportation Map

There are a lot of ways to travel between the 4 theme parks, 2 water parks, Downtown Disney and 20+ Disney resorts at Walt Disney World in Florida, but as far as I know there is no transportation map that shows all the different bus and monorail routes. As a passholder who loves to visit this happy place, I thought it would be fun to try and make a subway-style transit map of all Disney transportation at Walt Disney World. Read more

Birdorable Birds of Prey

In the last few weeks I’ve been working on making new birds of prey species for my website Some of the ones you see below are new, while others are updated versions of species that were already on the site. I like most of my raptors to have a ‘mean’ look, by giving them a prominent brow. A lot of raptors, like eagles and hawks, actually have a bony ridge above the eye. This not only makes them look tough, but it functions as a visor, shading the eye from the the bright sun and allowing it to see better when hunting. Isn’t that neat?

Quick and Easy Way to Add your Facebook App to a Page

To add an app to your page, Facebook’s documentation tells you to click the Add to Page link on the ‘about page’ of the app. Okay, but what if your app doesn’t have an about page? I was struggling with this the other day until I found this extremely handy URL:

Just replace api_key with the App ID of your application and Facebook will ask you what page to add the application to. Alternatively, you can also use this little form: