[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1861
  • Last Modified:

Remove non-specified characters from string?

I have a string, named $my_string.

I want to use PHP to remove all characters in $my_string except for upper and lower case letters, numbers, and double quotes.

I'm thinking that ereg_replace is the way to go (as opposed to preg*, as I don't know the details of the deployment server) - I know how to use ereg_replace to remove characters that I specify, but I don't know how to remove all characters EXCEPT for what I specify...

So, how do I remove all characters from a string except for those that I specify?
0
TheAnarchist
Asked:
TheAnarchist
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
amit_gCommented:
ereg_replace('[^a-zA-Z0-9"]', "", $YourString);
0
 
_Marcel_Commented:
Something like this:

$my_string = eregi_replace('[^a-z0-9"]', '', $my_string);

(untested)
0
 
Richard QuadlingSenior Software DeverloperCommented:
<?php
$s_Test = 'T.H.I.S. .".S.T.R.I.N.G.". .i.s, %6^ &L*E(T)T_E+R{S} [L]O:N@G~';
// amit_g
echo ereg_replace('[^a-zA-Z0-9"]', '', $s_Test), PHP_EOL;

// _Marcel_
echo eregi_replace('[^a-z0-9"]', '', $s_Test), PHP_EOL;

// preg_replace
echo preg_replace('`[^a-z0-9"]`i', '', $s_Test), PHP_EOL;
?>

outputs ...

THIS"STRING"is6LETTERSLONG
THIS"STRING"is6LETTERSLONG
THIS"STRING"is6LETTERSLONG

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!

 
TheAnarchistAuthor Commented:
Thank you for that. One more thing, though: I also want spaces (that is, usage of the space bar) to be preserved - I failed to mention this in my original question.

Can you alter the pattern to include spaces?
0
 
Richard QuadlingSenior Software DeverloperCommented:
<?php
$s_Test = 'T.H.I.S. .".S.T.R.I.N.G.". .i.s, %6^ &L*E(T)T_E+R{S} [L]O:N@G~';
// amit_g
echo ereg_replace('[^ a-zA-Z0-9"]', '', $s_Test), PHP_EOL;

// _Marcel_
echo eregi_replace('[^ a-z0-9"]', '', $s_Test), PHP_EOL;

// preg_replace
echo preg_replace('`[^ a-z0-9"]`i', '', $s_Test), PHP_EOL;
?>

outputs ...

THIS "STRING" is 6 LETTERS LONG
THIS "STRING" is 6 LETTERS LONG
THIS "STRING" is 6 LETTERS LONG
0
 
Richard QuadlingSenior Software DeverloperCommented:
The important bit is the expression.

Using the preg_replace one ...

`[^ a-z0-9"]`i

`...` are delimiters to the expression. This allows the i to be included with the expression but not part of the expression and acts as a modifier - in this case i means case insensitive (a === A).
[] is a set of characters or character ranges to match

^ as the first character of a set means NOT match.

space is a space.
a-z is a thru z, the i modifier makes it case insensitive
0-9 is 0 thru 9
" is "

So, [^a-z0-9"] means any characters which ISN'T space, a thru z or 0-9 or " will be matched.

And then replaced with blank.
0
 
_Marcel_Commented:
Couldn't have said it better...
0
 
TheAnarchistAuthor Commented:
Thank you for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now