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

Recommended Answers

All 4 Replies

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.

Ahhh okay :)
The replacement value would be
(empty quoted string).

Member Avatar

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.