Link to home
Start Free TrialLog in
Avatar of AndyPSV
AndyPSVFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to build a function (PHP) which replaces....

everything about: 0-9, a-z, A-Z, _
replaces on '' (deletes)
Avatar of dvz-
dvz-
Flag of United States of America image

I don't understand the question exactly...

You want a function that takes a string and deletes those characters?

Can you show a string to pass to the "function" and what the desired output would be ?
ASKER CERTIFIED SOLUTION
Avatar of profya
profya
Flag of Sudan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xBellox
xBellox

To replace everything that ISN'T alphanumeric and/or "_" you can use this:

$new_string = preg_replace(/[^a-zA-Z0-9_\s]/, , $string);

Like in this example:

<?php
 
$string = Here! is some text, and numbers 12345, and symbols !£$%^&;
 
$new_string = preg_replace(/[^a-zA-Z0-9_\s]/, , $string);
 
echo $new_string; // It shows: Here is some text and numbers 12345 and symbols
 
?>

Open in new window

And to test it:
echo myDelete("123 abdx **&&++__");

Open in new window