Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Remove All Special Characters And Replace Spaces With Hyphens

PHP Remove All Special Characters And Replace Spaces With Hyphens

This PHP function will use Regex (Regular expression) to remove all special characters and replace space with hyphens so your string only has letters and numbers. And then converts them all to lowercase. It makes your link friendly, good for SEO and great for naming your images/photos, url and attached file when publish them online.

PHP Function

<?php
	function clean($string) {
		$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
		$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
		$string = strtolower($string); // Convert to lowercase
 
		return $string;
	}
?>

<?php function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. $string = strtolower($string); // Convert to lowercase return $string; } ?>

Usage

<?php
 
	$str_input = "PHP Remove All Special Characters And Replace Spaces With Hyphens | 4 Rapid Development";
	$str_input = clean($string);
 
	echo $str_input; //php-remove-all-special-characters-and-replace-spaces-with-hyphens--4-rapid-development
 
?>

<?php $str_input = "PHP Remove All Special Characters And Replace Spaces With Hyphens | 4 Rapid Development"; $str_input = clean($string); echo $str_input; //php-remove-all-special-characters-and-replace-spaces-with-hyphens--4-rapid-development ?>

Mar 5, 2014Hoan Huynh

Demo page

Top 8 Magento extensions that’ll grow your E-store visitor countLawyers be Aware of These 5 Common Online Marketing Mistakes
You Might Also Like:
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • Javascript remove vietnamese accents
  • JQuery Allow only numeric characters or only alphabet characters in textbox
  • PHP UTF-8 Change Text To UPPERCASE – Text To lowercase – Capitalize Each Word – UPPERCASE First Character
  • PHP remove vietnamese accents
  • Replace String In MySQL
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • C# ASP.NET Remove leading and trailing quotes(‘) or double quotes (“)
  • Replace String With NText Or Text Data Type In MS SQL Server
  • JQuery Create Textbox With Count Down Limit Characters
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

Link7 years ago PHPlowercase, preg_replace, Regex, regular expression, str_replace, strtolower5,080
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,210 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,077 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,854 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,334 views
C# Read Json From URL And Parse/Deserialize Json
9,828 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Photo Editor App – How Good Is it?

  • The Best Way To Write An Essay – Creating A Good Essay
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (114)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development