ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

PHP program to remove special characters from a string

Here, we are going to implement a PHP program to remove special characters from a string.
Submitted by Sayesha Singh, on August 10, 2020

Characters other than alphabets and numbers are called special characters. And they can have used more than one which is doesn't happen in the case of alphabets or numbers.

1) Using preg_replace()

This is the most used method. A pattern can be inputted which will be replaced everywhere in the string.

preg_replace() function can be used in PHP for searching and replacing. It always returns either a string or an array, according to what was searched and replaced.

Syntax:

    preg_replace(
        $word_to_replace, 
        $new_word, 
        $string_to_be_searched, 
        $limit, 
        $count
        )

In the given example we have a sentence from which every special character including space is removed,

Program:

<?php
    #string
    $str = 'We welcome all of you to our school (of PHP).
    This--> school is one of its kind :).
    Many schools dont offer this subject :(';
    
    $str = preg_replace('/[^A-Za-z0-9]/', '', $str);
    
    // Printing the result
    echo $str;
?>

Output:

WewelcomeallofyoutoourschoolofPHPThisschoolisoneofitskindManyschoolsdontofferthissubject

2) Using str_replace()

This is not common but it has its own importance. This differs from the first method in the sense that here programmer defines the special characters. He also decides what should the special characters be replaced with if the program encounters it.

str_replace() function is used for searching a regular expression and replacing a specific content in place of all the occurrences of searched expression. It always returns either a string or an array, according to what was searched and replaced. It has three mandatory parameters while one is optional. The compulsory parameters of the function are - value to be searched, the value it will be replaced with and the string in which this value needs to be searched. The optional parameter specifies the count of the number of times the replacement is needed.

Syntax:

    str_replace(
        $word_to_replace, 
        $new_word, 
        $string_in_which_word_is_present
        )

Program:

<?php
    #string 
    $str= 'We, welcome all of you, to our school <of PHP>.
    This--> school is one of its kind :).
    Many schools dont offer this subject :(';

    $str = str_replace( array( '\'', '(',')', ',' , ':', '<', '>','.',' ','-' ), '',$str); 

    // Printing the result 
    echo $str;  
?>

Output:

WewelcomeallofyoutoourschoolofPHP
Thisschoolisoneofitskind
Manyschoolsdontofferthissubject

PHP String Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.