Jump to content

[SOLVED] Retrieving letters from a string


point86

Recommended Posts

<?php

$string = strtoupper($string);

?>

 

That will convert everything to upper case.

 

Then you can use a replace function to remove any non letter characters, I'm not the right one to show you how to do that because I'm not an expert on regular expressions yet. But look in to preg_replace

i think this is what you want:

<?php
        
        function convert($file){
                $fp = fopen($file, 'r')
                while(!feof($fp)){
                        $line = fgets($fp, 1024);
                        $line = strtoupper($line);
                        $line = str_replace(" ", "", $line);
                        $line = str_replace(".", "", $line);
                        $string[] = $line;
                }
                fclose($fh);
                return $newstring = explode("\n", $string);
        }

        /*execute function*/
        convert("your_file.txt");
?>

Beware that it doesn't cover the etc part...

LETTERS (not spaces, periods etc)

 

If you only want letters, you'll need a regular expression. You'll also need to use Unicode properties if there are letters outside of the A-z range.

 

i definitely would have used preg_replace() instead of a couple of str_replace()'s, but unfortunately i am unfamiliar with regex =\. i need to learn it, otherwise i'm only half as capable. do you have any suggestions for tutorials or articles or books to learn regex, effigy?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.