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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help installing Laravel app on MAMP on MAC 7 58
How to confirm and grey the submit button using JavaScript ? 14 71
Load string Array from file 23 42
php time 12 30
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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