Solved

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

Posted on 2006-06-15
8
5,751 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
[X]
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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Security 29 71
Seeking WordPress plugin for logged in users to be able to upload required documents 6 97
Wordpress Pagination 1 44
Change a button after e-mail 4 19
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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…

740 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