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

everything about: 0-9, a-z, A-Z, _
replaces on '' (deletes)
AndyPSVAsked:
Who is Participating?
 
profyaConnect With a Mentor Commented:

function myDelete($text)
{
	$pattern="/[0-9A-Za-z_]/";
	return preg_replace($pattern, "", $text);
}

Open in new window

0
 
dvz-Commented:
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 ?
0
 
xBelloxCommented:
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

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

Open in new window

0
All Courses

From novice to tech pro — start learning today.