frankienrg Posted September 25, 2009 Share Posted September 25, 2009 Hi, i'm new here, I need help with some coding i'm doing right now. I need to pick up words written only in capital letters from strings. For example, I got this list. DEL GROSSO Cristiano KOLAROV Aleksandar THIAGO SILVA DE SANCTIS Morgan FREY Sebastian CAMBIASSO Esteban Matias JULIO CESAR Soares De Espinola LUCIO these are stored in a file. When my scripts gets these names from the file, I want it to print them on screen like that: DEL GROSSO KOLAROV THIAGO SILVA DE SANCTIS FREY CAMBIASSO JULIO CESAR LUCIO I tried many things, but couldn't come in anyway with this result I may say, I cannot modify the file from where I get these names. thx in advance for your help! Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted September 25, 2009 Share Posted September 25, 2009 This way you can atleast get the capitalized words from the context. I don't think there is anyway from a STRING to maintain the lines also, but I might be wrong. <?php $str = 'DEL GROSSO Cristiano KOLAROV Aleksandar THIAGO SILVA DE SANCTIS Morgan FREY Sebastian CAMBIASSO Esteban Matias JULIO CESAR Soares De Espinola LUCIO'; preg_match_all('/[A-Z][^a-z ]+/', $str, $m); var_dump($m); Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 This way you can atleast get the capitalized words from the context. I don't think there is anyway from a STRING to maintain the lines also, but I might be wrong. <?php $str = 'DEL GROSSO Cristiano KOLAROV Aleksandar THIAGO SILVA DE SANCTIS Morgan FREY Sebastian CAMBIASSO Esteban Matias JULIO CESAR Soares De Espinola LUCIO'; preg_match_all('/[A-Z][^a-z ]+/', $str, $m); var_dump($m); each line is a string I grab the strings one by one (using a for cycle), will try the code you posted, and let you know asap Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted September 25, 2009 Share Posted September 25, 2009 This will also keep the line order but it reads the data from file: <?php $fh = fopen('test.txt', 'r'); $lines = array(); while(!feof($fh)) { $lines[] = fgets($fh); } foreach ($lines as $key => $line) { $lines[$key] = preg_replace('/([a-z]|[A-Z]{1}[a-z])/', '', $line); } $newString = implode("", $lines); echo '<pre>'; print_r($newString); echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 This will also keep the line order but it reads the data from file: <?php $fh = fopen('test.txt', 'r'); $lines = array(); while(!feof($fh)) { $lines[] = fgets($fh); } foreach ($lines as $key => $line) { $lines[$key] = preg_replace('/([a-z]|[A-Z]{1}[a-z])/', '', $line); } $newString = implode("", $lines); echo '<pre>'; print_r($newString); echo '</pre>'; what's $line for? Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 i'm posting the code <?php $utenti = @file("utenti.php"); $linee = count($utenti); for($num1 = 1 ; $num1 < $linee; $num1++) { @list($outente, $opass) = explode("<del>", $utenti[$num1]); ############################################################ $calciatori = @file("calciatori.txt"); $num_calciatori = count($calciatori); for ($num2 = 0 ; $num2 < $num_calciatori ; $num2++) { $dati_calciatore = explode(",", $calciatori[$num2]); $numero = $dati_calciatore[0]; $ruolo = $dati_calciatore[2]; $proprietario = $dati_calciatore[4]; if ($proprietario == $outente) { $nome = stripslashes($dati_calciatore[1]); $ruolo = $dati_calciatore[2]; $costo = $dati_calciatore[3]; ...... ......#Code continues... ?> this is what the input file (calciatori.txt) looks like 859,MACCARONE Massimo,A,18,Angel,200909222152 530,CANDREVA Antonio,C,12,Angel,200909222152 857,LAVEZZI Ezequiel,A,23,CiccioRi,200909222148 528,CAMBIASSO Esteban Matias,C,22,CiccioRi,200909222148 683,THIAGO MOTTA,C,23,CiccioRi,200909222148 377,GROSSO Fabio,D,13,CiccioRi,200909222147 905,VUCINIC Mirko,A,23,Groto,200909222118 649,PASTORE Javier,C,14,Groto,200909222118 611,MANNINI Daniele,C,17,Groto,200909222117 the variable in question is $nome, unfortunately, I didn't understand how to get only the capital words! Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted September 25, 2009 Share Posted September 25, 2009 859,MACCARONE Massimo,A,18,Angel,200909222152 530,CANDREVA Antonio,C,12,Angel,200909222152 857,LAVEZZI Ezequiel,A,23,CiccioRi,200909222148 528,CAMBIASSO Esteban Matias,C,22,CiccioRi,200909222148 683,THIAGO MOTTA,C,23,CiccioRi,200909222148 377,GROSSO Fabio,D,13,CiccioRi,200909222147 905,VUCINIC Mirko,A,23,Groto,200909222118 649,PASTORE Javier,C,14,Groto,200909222118 611,MANNINI Daniele,C,17,Groto,200909222117 Okey, so you have this text in variable or in file or where? Please tell that and also if you can you could post the desired output from this also. Will the output be in variable printed to a www page or saved to a file or what? The last example I posted reads a file line by line, then goes through every line and leaves only capitally written words to the lines, and in the end the implode function will recunstruct the a string with new lines in it from the 'only capitalized words' context. Keeping the line order as it was before. Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 i have this text in calciatori.txt, if you check the code i provided, it makes an array for each line on the file ($dati_calciatore). it is echoed later in the script (had no problems echoing it the way the code displays it) Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted September 25, 2009 Share Posted September 25, 2009 So is this the format always for your data? And you want the capitalized word/name after the few numbers only to stay ? Like this? <?php $fh = fopen('calciatori.txt', 'r'); $lines = array(); while(!feof($fh)) { $lines[] = fgets($fh); } foreach ($lines as $key => $line) { echo preg_replace('/[0-9]+,([A-Z]+).*/', "$1<br/>", $line); } Output: MACCARONE CANDREVA LAVEZZI CAMBIASSO THIAGO GROSSO VUCINIC PASTORE MANNINI Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 I appreciate your help, but obviously you didn't read the code. as you can see your outputs only "THIAGO" instead of "THIAGO MOTTA". I can't change the way the file is opened, and the way I threat the array. The file's list is much bigger (about 500 entries), and I don't need to echo only the First part of the name, but the whole capitalized name. Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 I guess I should make it more simple; I got: $string1= "JULIO CESAR Soares De Espinola"; $string2= "GROSSO Fabio"; $string3= "DE ROSSI Daniele"; $string4= "CAMBIASSO Esteban Matias"; $string5= "LUCIO"; $string6= "THIAGO MOTTA"; [code] What I want to do is to obtain this: [code] $final_string1= "JULIO CESAR"; $final_string2= "GROSSO"; $final_string3= "DE ROSSI"; $final_string4= "CAMBIASSO"; $final_string5= "LUCIO"; $final_string6= "THIAGO MOTTA"; [code] I want to obtain this with an automated function. Hope you can help me Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 25, 2009 Share Posted September 25, 2009 preg_match("~[\d\s]+~", $string1, $M); $string1 = $M[1]; Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 25, 2009 Share Posted September 25, 2009 Oh, sorry, I mean [A-Z\s]+ Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 25, 2009 Share Posted September 25, 2009 Also, this code will grab it all from the text file (Tested, it works) <?php $filename = "Test.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); preg_match_all("~((?:[A-Z]+\s)+)~m", $contents, $M); $M = $M[0]; print_r($M); ?> Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 this preg_match("~[A-Z\s]+~", $string1, $M); $string1 = $M[1]; returns nothing this preg_match_all("~((?:[A-Z]+\s)+)~m", $contents, $M); $M = $M[0]; this returns "Array". using the last code you provided, if i use preg_match, instead of preg_match_all, I get ALMOST perfect result... but not for one string.. $string5= "LUCIO"; after the preg_match returns nothing! Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 seems like your code doesn't work if there's just one word in the string, fixed with this: $string = ""; //Here Goes the string $string_count = count(explode(' ', $string)); if ($string_count == 1) $output = $string; else { preg_match("~((?:[A-Z]+\s)+)~m", $string, $output); $output = $output[0]; } Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 25, 2009 Share Posted September 25, 2009 I know it returns an array, that's the point. You're meants to implode the array yourself. Here, I made the whitespace optional and added a negative lookahead to discount names that started with a capatal but were not <?php $filename = "Test.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); preg_match_all("~((?:[A-Z]+(?![a-z])\s?)+)~m", $contents, $M); $M = $M[0]; $M = implode("\n", $M); print_r($M); ?> So, do you want it to match ALL uppercase letters, or only uppercase WORDS? $M is now a string, like it's orriginal form, but without the lowercase names Quote Link to comment Share on other sites More sharing options...
frankienrg Posted September 25, 2009 Author Share Posted September 25, 2009 only uppercase words! your code works like a charm now! thx for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.