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! Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/ 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); Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924900 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 Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924909 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>'; Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924922 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? Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924925 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! Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924936 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. Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924953 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) Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924958 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 Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-924963 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. Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925003 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 Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925059 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]; Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925064 Share on other sites More sharing options...
Garethp Posted September 25, 2009 Share Posted September 25, 2009 Oh, sorry, I mean [A-Z\s]+ Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925066 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); ?> Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925067 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! Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925074 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]; } Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925075 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 Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925077 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! Link to comment https://forums.phpfreaks.com/topic/175521-solved-how-to-grab-only-capital-words-from-strings/#findComment-925085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.