Greetz to you all
Well i have an array that contain some characters and i want to check if they form part of a string.If yes then they should be deleted
How to do this via php
For e.g

$string = 'Dani.web';
$charRemove = array('.','-','~','#','!','(',')','+',',');

I want to get a piece of code that will remove the '.' from the string and make it as
Note:I just want to delete the characters in array $charRemove that form part of string.There might be multiple occurences of any character of in $charRemove.

Thanks for the help

4 Years
Discussion Span
Last Post by diafol

I thought of that but i am confused about what would be the replacement value in this case

The replacement value that replaces found search values. An array may be used to designate multiple replacements.


You can use an array of chars as the 'search for' or 'needle' parameter and '' as the 'replace with' parameter. The subject parameter is your virgin text or the 'haystack'.

$text = 'Al@Cemegol.23-89#';
$cleantext = str_replace(array('@','.',',','#'), '', $text);

or use preg_replace to just keep alphanumerics (a-z,A-Z,0-9), stripping all other chars.

$cleantext = preg_replace('/[^a-zA-Z0-9]/', '', $text);

OR even

$cleantext = preg_replace('/[^\da-z]/i', '', $text);

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.