1. Home
  2. Articles
  3. Web Development
  4. PHP
  5. Article Title Conversion to Friendly URL Using PHP String Replace and RegEx

Article Title Conversion to Friendly URL Using PHP String Replace and RegEx

Published:

Share

Description

There are a couple of items that I will need to address with the dynamic article system. One of those items is providing a easy way to parse out web page names from a title. This should be pretty easy with some regex and string replace php functions.

Article

The purpose of this note is to test the automation process for correctly converting an article title in to a friendly URL. The process uses the PHP str_replace function first to convert spaces to hyphens. Then a the PHP RegEx (preg_replace) furhter filters the article title to alphanumeric and spaces.

Might as well test out the TinyMCE code insert as well.

$myArticleURL = preg_replace("/[^a-zA-Z0-9-]+/", "", str_replace(' ', '-', $_POST['txtArticleTitle']));

If all goes well, the defaulting for article URLs will be complete. I will add some polish by allowing the ability to override the defaulted value with a custom value entered by the user. Maybe later I will look into a dynamic JavaScript or jQuery solution that could replace the server side PHP function with a client side solution.

:D

Author

Tags

  1. Articles
  2. Web Development
  3. PHP

Related Articles