-2

I have a huge text file, and I want to remove everything that isn't a symbol (for example - , . ' "), how do I remove everything so that the only things that are left is symbols, without specifying what symbols to keep.

As an example

"There's a man outside. He's come to take you away!"

would leave you with "'.'!"

Dave Melia
  • 307
  • 2
  • 5
  • 17

4 Answers4

2

Use regular expressions:

$outputText = preg_replace('/[a-z0-9]+/Ui', '', $inputText);
SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
  • What is the difference between preg_replace and str_replace? Is it better practice to use one over the other? – Dave Melia Jun 06 '13 at 13:26
  • with `preg_replace`, you can use regular expressions. If you don't know what a RegEx is, I suggest you to find out, for it is way more powerfull than simple text search and replace, by far. http://www.regular-expressions.info/tutorial.html – SteeveDroz Jun 06 '13 at 13:32
1
preg_replace("/[A-Za-z0-9+]/", '', $string);
Mohammad Masoudian
  • 3,483
  • 7
  • 27
  • 45
0

Try this (untested, just freestyled into this window here):

implode('', array_filter(str_split([.. your string ..]), function ($char) { return !ctype_alnum($char); }));

0

I went with

$symbCount = preg_replace('/[A-Za-z0-9+]/', '', $fileStr);

Thanks

Dave Melia
  • 307
  • 2
  • 5
  • 17