Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

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

everything about: 0-9, a-z, A-Z, _
replaces on '' (deletes)
0
AndyPSV
Asked:
AndyPSV
  • 2
1 Solution
 
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
 
profyaCommented:

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

Open in new window

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now