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

Posted on 2009-07-14
Last Modified: 2013-12-13
everything about: 0-9, a-z, A-Z, _
replaces on '' (deletes)
Question by:AndyPSV
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

Expert Comment

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 ?
LVL 14

Accepted Solution

profya earned 500 total points
ID: 24852987

function myDelete($text)
	return preg_replace($pattern, "", $text);

Open in new window


Expert Comment

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:

$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

LVL 14

Expert Comment

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

Open in new window


Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

635 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