Jump to content

[SOLVED] Retrieving letters from a string


point86

Recommended Posts

Hi,

 

If I have read in a file, which I then convert into a string (say "string1"), how do I make a loop that goes through the string, makes all the letters capitals and writes all the LETTERS (not spaces, periods etc) into a new string ("string2")?

 

Thanks

 

Point.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.