Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

Converting page titles to URL friendly titles

Code Wizard, 12-30-2006

This is the conversion method used by evil walrus to convert script titles to URL titles. Removes all special characters, except hyphens, underscores, periods, commas and forward slashes, which are converted to underscores. Two or more consecutive underscores are converted to a single underscore.

<?php

// written by Aaron Hall, evilwalrus.org
// free to use, modify and redistribute without notification

$title = "This is a script -- uses PHP/MySQL! __cool!__ ";
$title = ereg_replace("[^A-Za-z0-9_-./, ]", "", $title); // remove special characters
$title = str_replace(array('.','-','/',','), " ", $title); // replace legal characters with spaces
$title = trim($title); // remove leading and trailing whitespace
$title = ereg_replace(" {1,}", "_", $title); // convert one or more consecutive spaces to a single underscore
$title = ereg_replace("_{2,}", "_", $title); // convert two or more consecutive underscores to a single underscore

echo $title;
// prints "This_is_a_script_uses_PHP_MySQL_cool";

?>
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions