Solved

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

Posted on 2006-06-15
8
5,753 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:Calvin Close
[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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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:Calvin Close
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

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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