Solved

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

Posted on 2006-06-15
8
5,745 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
 
LVL 49

Expert Comment

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

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

-r-
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now