Jump to content

[SOLVED] How to "grab" only CAPITAL words from strings


frankienrg

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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