Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop through multiple arrays 13 28
error log using ftp 7 41
Is there a simpler dropbox system? 10 22
Setting up JqueryUI with MVC 5 in Visual Studio 5 3
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

777 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