Solved

Regular Expressions - Remove all non-alpha except replace * with %

Posted on 2006-06-15
8
5,747 Views
Last Modified: 2011-09-20
I want to remove all (multiple) non-alpha characters in $string, except that I want to replace all occurences of '*' with a '%'

For example '1234-56.*.90*' would be changed to '123456%90%'

Needs to be a PHP function (PHP v4.4.x)


Calvin

0
Comment
Question by:calvinclose
8 Comments
 
LVL 5

Accepted Solution

by:
aminerd earned 250 total points
ID: 16915750
// remove non-alpha-numeric characters (except %)
$string = preg_replace('/[^a-zA-Z0-9*]/', '', $string);

// replace % with *
$string = str_replace('*', '%', $string);
0
 
LVL 5

Expert Comment

by:aminerd
ID: 16915757
(My last comment is actually backwards.. I'm replacing * with %)
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16916161
A tiny bit shorter would be
$string = preg_replace('/([^\w\d*])/', '', $string);
$string = str_replace('%', '*', $string);
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 49

Expert Comment

by:Roonaan
ID: 16917596
@TeRReF: \w = \d | [a-z][A-Z] | _

$string = str_replace('*','%', preg_replace('/[^\d\*]/','',$string));

-r-
0
 
LVL 5

Expert Comment

by:aminerd
ID: 16917812
Roonaan, won't [^\d\*] replace any alpha characters?
0
 
LVL 6

Expert Comment

by:ixti
ID: 16917834
calvinclose if you need to strip up speces too, then aminerd's solution is for you.
TeRReF's solution is actually not what you need, because \w includes underscope "_" and as I understood you don't want them.

So:
<?php
// if you want to strip spaces
$string = preg_replace('/[^a-z0-9*]/i', '', $string);

// if you don't, then comment string above and uncomment this string:
// $string = preg_replace('/[^a-z 0-9*]/i', '', $string);

$string = str_replace('*', '%', $string);
?>
0
 
LVL 6

Expert Comment

by:ixti
ID: 16917853
Sorry, when Iv'e opened a question, there were no Roonaan's solution.
Roonaan's solution is great, and if you want to add alpha chars to you can simply add a-z to regular expression of Roonaan:
$string = str_replace('*','%', preg_replace('/[^a-z\d\*]/i','',$string));
0
 

Author Comment

by:calvinclose
ID: 17033599
I have accepted aminerd's solution because it did exactly what I wanted - remove all occurences of chars that are not (A-Z, a-z or *), then replace * with %.

I apologise if some of the other fancier solutions do the same thing - I am a novice at regular expressions, so chose the first solution that met my requirements.

Thanks Everyone,


Calvin
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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