So I have a text file that has a persons name, email message and email address on each line. I need a way to output only the name and the email message, while just ignoring the the email address on the end.
My text file looks like this.
Joe Hello how are you? joe@yahoo.com
Jim Whats up? jim@yahoo.com
Megan How is everyone doing? megan@yahoo.com
I was using a foreach loop to print out each line, but it of course keeps the email address, so how would I do this but not add the email address?
$messages = file('messages.txt');
foreach($messages as $message)
{
echo $message;
}
As I said in comment - find position of a last space in a string and get a substring from beginning to a found position:
$messages = file('messages.txt');
foreach($messages as $message)
{
echo substr($message, 0, strrpos($message, ' ')) . '<br />';
}
$txt=file_get_contents("Duomenys.txt");
$lines=explode("
",$txt);
for($i=0;$i<count($lines);$i++)
{
$message="";
$words=explode(" ",$lines[$i]);
for($x=0;$x<(count($words)-1);$x++)
{
$message.=$words[$x];
}
echo "$message<br>";
}
Or break down every line to words and echo all except the last one