Unbreakable Unbreakable - 5 months ago 12
PHP Question

How to read only A-Z and a-z from a PHP file

I want to read only the alphabetic content from a file and neglect the rest of the special characters. How to achieve this?

I intend to use

$content = file_get_contents($argv[1]);


How can I make sure
$contents
does not contain any special character.

Answer

If you need to extract only A-Z and a-z, use this:

test.txt

abcdzjsi3424mmdnsn12312312

test.php

$content = file_get_contents('test.txt');
$only_az = preg_replace('/[^a-zA-Z\s]/', '', $content);
echo $only_az; //Output: abcdzjsimmdnsn

Hope this works.