This should do what you’re looking for:
function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. }
Usage:
echo clean('a|"bc!@£de^&$f g');
Will output: abcdef-g
Edit:
Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1?
function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one. }
Related Posts:
- regex match any whitespace
- Get the full URL in PHP
- Get the full URL in PHP
- Get the full URL in PHP
- how to replace quotation marks with \”
- Regex to check for new line
- what does a .php?id=&value=value means?
- Check if URL has certain string with PHP
- What is the MM/DD/YYYY regular expression and how do I use it in php?
- How get value from URL
- What is a slug?
- What’s the net::ERR_HTTP2_PROTOCOL_ERROR about?
- What are namespaces?
- S_SESSION variable (user role) not recognised
- Is SAJAX dead? What to replace with?
- How do I resolve a HTTP 414 “Request URI too long” error?
- Fatal error: Call to undefined function mysqli_connect()
- What is difference between PHP cli and PHP cgi?
- Why use $_SERVER[‘PHP_SELF’] instead of “”
- phpMyAdmin: secret passphrase?
- PHP how to make URL something like product.php?id=1
- Mixing a PHP variable with a string literal
- Forbidden :You don’t have permission to access /phpmyadmin on this server
- How do I run a file on localhost?
- How to insert an item at the beginning of an array in PHP?
- PHP Deprecated: Methods with the same name
- How to call a JavaScript function from PHP?
- How do I pass JavaScript variables to PHP?
- Call to undefined function curl_init().?
- How to verify password against database?
- what is a good method to sanitize the whole $_POST array in php?
- Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 71 bytes)
- Difference between $_SERVER[‘DOCUMENT_ROOT’] and $_SERVER[‘HTTP_HOST’]
- Refresh a page using PHP
- Unable to locate package libapache2-mod-php7
- Laravel 5.2 Storage::makeDirectory($dir) is not creating directory
- Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0
- SSL error SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
- PDO closing connection
- “Valid CSRF Token Required” in Osticket After login?
- Child theme css not overriding parent
- Convert timestamp to readable date/time PHP
- Convert a date format in PHP
- Get user role by ID WordPress
- How to fix syntax error, unexpected T_IF error in php?
- NotFoundHttpException in RouteCollection.php line 161: in laravel 5
- Easy way to password-protect php page
- How can I capture the result of var_dump to a string?
- Is Angularjs Clientside or Serverside or Both
- Laravel 5 Class ‘Collective\Html\HtmlServiceProvider’ not found on AWS
- MySQL query to get column names?
- Fatal error: Out of memory, but I do have plenty of memory (PHP)
- Changing permissions via chmod at runtime errors with “Operation not permitted”
- php – How do I fix this illegal offset type error
- Getting ’ instead of an apostrophe(‘) in PHP
- WooCommerce action hooks and overriding templates
- Updating php version on mac
- How to send a GET request from PHP?
- WRONGTYPE Operation against a key holding the wrong kind of value php
- Unzip a file with php
- What is the advantage of using try {} catch {} versus if {} else {}
- Difference between require, include, require_once and include_once?
- What does the PHP error message “Notice: Use of undefined constant” mean?
- select * from table where column = value ^ column2= value
- Why I get “ERR_RESPONSE_HEADERS_TOO_BIG” on chrome?
- Object of class mysqli_result could not be converted to string
- How to write a link like which link to the same page in PHP?
- Download files from server php
- No query results for model [App\Products] Laravel
- Sending POST data without form
- Resetting MySQL Root Password with XAMPP on Localhost
- PHP array delete by value (not key)
- PHP – syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
- PHP mail: Multiple recipients?
- Having a problem getting mysqli_query to execute
- How can I force users to access my page over HTTPS instead of HTTP?
- How to install a specific version of package using Composer?
- A non well formed numeric value encountered
- Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04
- Access PHP variable in JavaScript
- CSS background images in WordPress
- Reset PHP Array Index
- Is there any difference between aes-128-cbc and aes-128 encryption?
- Convert timestamp to readable date/time PHP
- In where shall I use isset() and !empty()
- How can you use php in a javascript function
- How to bypass (deprecated) reCAPTCHA V1?
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ””)’ at line 2
- php, cookies, wordpress – how to make automatic login in one one site, login and on other one
- WordPress get_template_directory_uri() not returning https and only return http
- Get page content using slug
- How do you use WP slugs for PDF files / media?
- Extend PHP regex to cover “srcset” and “style” attributes
- get_users meta_query: REGEXP not working for matching new lines
- Remove slugs from custom posts type
- Getting the slug into a variable, how to echo that variable
- php string inside shortcode does not work
- get_category only returning details for ‘uncategorized’
- Echo author slug in post edit page
- How to cut a section of a URL from a string with a regular expression?