Replace special character in php
UpdateThe solution below has a "SEO friendlier" version: Show
The rationale for the above functions (which I find way inefficient - the one below is better) is that a service that shall not be named apparently ran spelling checks and keyword recognition on the URLs. After losing a long time on a customer's paranoias, I found out they were not imagining things after all -- their SEO experts [I am definitely not one]
reported that, say, converting "Viaggi Economy Perù" to There were also some common misspellings that seemed to influence the results, and the only explanation that made sense to me is that our URL were being unpacked, the words singled out, and used to drive God knows what ranking algorithms. And those algorithms apparently had been fed with UTF8-cleaned strings, so that "Perù" became "Peru" instead of "Per". "Per" did not match and sort of took it in the neck. In order to both keep UTF8 characters and replace some misspellings, the faster function below became the more accurate (?) function above. Previous answerA simple approach:
Note that you might have to first So the finished function along with test cases:
To handle UTF-8 I used a The function above also implements converting to lowercase - but that's a taste. The code to do so has been commented out. How can I replace special characters in a string in PHP?The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.
How do I remove numbers and special characters from a string in PHP?Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “). Example: This example illustrates the use of the str_replace() function to remove the special characters from the string.
How do I remove special characters from a string?Example of removing special characters using replaceAll() method. public class RemoveSpecialCharacterExample1.. public static void main(String args[]). String str= "This#string%contains^special*characters&.";. str = str.replaceAll("[^a-zA-Z0-9]", " ");. System.out.println(str);. What is special character in PHP?Special characters in HTML are represented by entities such as & and < . There are two PHP functions that turn special characters in a string into their entities: one for removing HTML tags, and one for extracting only meta tags.
|