Solved

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

Posted on 2009-07-14
5
186 Views
Last Modified: 2013-12-13
everything about: 0-9, a-z, A-Z, _
replaces on '' (deletes)
0
Comment
Question by:AndyPSV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 5

Expert Comment

by:dvz-
ID: 24852913
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
 
LVL 14

Accepted Solution

by:
profya earned 500 total points
ID: 24852987

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

Open in new window

0
 
LVL 9

Expert Comment

by:xBellox
ID: 24852988
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
 
LVL 14

Expert Comment

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

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question