There are many ways to use str_replace function.

Let's take a look at the function first.

preg_replace

The preg_replace() function performs a regular expression searches a string for matches to a pattern and replaces them with replacement.

Syntax:


preg_replace(mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed

Simple Syntax:


preg_replace(the value to find, the value to replace, string to be searched, the maximum possible replacements for each pattern, the number of replacements)

Where first 3 parameters are required.

str_replace

The str_replace() function replaces some characters with some other characters in a string.

Syntax:


str_replace(mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed

Simple Syntax:


str_replace(the value to find,the value to replace,string to be searched,the number of replacements)

Where first 3 parameters are required.

strtolower

It makes the string lowercase.

Remove all special characters from a string


$cleanString = strtolower($string); 
$cleanString = preg_replace('/[^a-z0-9 -]+/', '', $cleanString);
$cleanString = str_replace(' ', '-', $cleanString);

OR make a function so that you can use it number of times


function cleanInputs($string) {
   $string = str_replace(' ', '-', $string); 
   return preg_replace('/[^A-Za-z0-9-]/', '', $string); 
}

It's up to you if you want to remove the hyphens first or after removing the special characters.

NOTE: You can replace it with lowercase or any other letter you like. I've used hyphens just for example.

There is a problem though. The solution above leaves multiple hyphens in the solution.

Replace multiple hyphens with one 


$finalOutput = preg_replace('/-+/', '-', $string); 

How to remove dash form any string?


$stringWithoutDash = str_replace("-", " ", $string);

You can remove any special characters you like. One at a time if your code requires that.


$string = str_replace("-", " ", $string);
$string = str_replace("?", " ", $string);
$string = str_replace("!", " ", $string);

Just like above example, it won't replace all the special characters but only 3.

Sources:

  • https://www.php.net/manual/en/function.str-replace.php
  • https://www.php.net/manual/en/function.strtolower.php
  • https://www.php.net/manual/en/function.preg-replace.php

13 Comments

Ganny K

Aug 08, 2020 08:08:06 am

This is my first time pay a visit at here and i am genuinely impressed to read everthing at one place.

Madeline

Jun 26, 2020 07:06:56 am

You've made some good points there. I looked on the web for more info about the issue and found most people will go along with your views on this web site.

Ramsay

Jun 17, 2020 08:06:22 am

That is a really good tip especially to those new to the blogosphere. Short but very accurate information… Thanks for sharing this one. A must read post!

Roshni

Jun 16, 2020 06:06:53 pm

It's hard to find educated people for this topic, however, you sound like you know what you're talking about! Thanks

g3

Jun 15, 2020 10:06:54 pm

Hey there, You have done a great job. I will certainly digg it and personally suggest to my friends. I'm sure they'll be benefited from this site.

Jani

Apr 30, 2020 02:04:15 pm

Magnificent beat ! І wоuld likе to apprentice wһile you amend yօur website, hoԝ cоuld i subscribe for a blog web site? The account helped me а acceptable deal. I had been a littⅼe bit acquainted off tһіs y᧐ur broadcast provided bright clear concept

Kumu

Apr 11, 2020 06:04:05 pm

What's up, I would like to subscribe for this weblog to obtain hottest updates, thus where can i do it please help out.

Will

Apr 11, 2020 09:04:18 am

I am in fact grateful to the owner of this web page who has shared this enormous post at here.

Kevin

Apr 11, 2020 03:04:22 am

Keep on writing, great job!

Murphy mike

Apr 09, 2020 08:04:08 am

For latest information you have to visit world-wide-web and on internet I found this website as a best web site for most up-to-date updates.

Texas

Apr 08, 2020 04:04:22 am

Hello! I've been reading your website for a long time now and finally got the courage to go ahead and give you a shout out from Kingwood Texas! Just wanted to say keep up the excellent job!

Mika

Apr 07, 2020 11:04:11 pm

Hi! Someone in my Facebook group shared this website with us so I came to take a look. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Terrific blog and superb design.

Hello

Apr 05, 2020 02:04:47 am

Hi, I log on to your new stuff like every week. Your story-telling style is witty, keep it up! Wow, this piece of writing is good, my sister is analyzing such things, therefore I am going to tell her. Hey would you mind stating which blog platform you're using? I'm planning to start my own blog in the near future but I'm having a hard time selecting between Wordpress and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something unique. P.S Apologies for getting off-topic but I had to ask!

Leave a reply

Your email address will not be published. required fields are marked *