Ok the code works fine when the $contents is just typed on the page itself but when it's called from an external file that's when the space is still there.
Here's the code that does the section that has the trailing whitespace problem:-
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded.<br /><br />";
} else{
echo "There was an error uploading the file, please try again!";
}
//path to file
$uppedfile = $target_path;
//declare variable
$contents = file_get_contents($uppedfile);
$stripped = preg_replace('/\n2 CONC /','', $contents);
//Searching for items that begin with 0 and end with 0 @ and 0 TRLR. Then putting what's inbetween into array
preg_match_all('/(?<=0).*(?=0 @|0 TRLR)/Us', $stripped, $m);
foreach ($m[0] as $va) {
//Get individual number
preg_match_all('/(?<=@).*(?=@ INDI)/Us', $va, $indi);
foreach ($indi[0] as $vb){
echo "<br />ID: $vb <br />";
}
//Get Note full,
preg_match_all('/(?<=1 NOTE).*(?=1 SOUR)/Us', $va, $note);
foreach ($note[0] as $vu) {
//get first note line
preg_match_all('/(?<=^).*(?=\n)/Us', $vu, $n1);
foreach($n1[0] as $vv){
echo "Notes: $vv<br />";
}
//Get the rest
preg_match_all('/(?<=2 CONT).*(?=\n)/Us', $vu, $n2);
foreach ($n2[0] as $vw) {
echo "Notes: $vw<br />";
}
}
}
[\PHP]
Sample from .txt file:-
0 @19254694@ INDI
1 NAME Earl* Alexander Miller /Chapman/
1 SEX M
1 BIRT
2 DATE 25 FEB 1908
2 PLAC Inverurie/Aberdeenshire, Scotland
1 DEAT
2 DATE 9 JAN 1988
2 PLAC Woodend Hospital Aberdeen
1 OCCU
2 PLAC Master Baker/ Railway Porter(retired)
1 NOTE Statutory Birth, 204/00 0035:
2 CONT 1908, District of Inverurie, County of Aberdeen:
2 CONT Earl Alexander Miller-Chapman, (Illegitimate).
2 CONT 1908, February Twenty-fifth, 6h. 0m. A.M.
2 CONT 11 Blackhall Road, Inverurie. Male.
2 CONT Parents, No Father Noted.
2 CONT Mary Chapman, Domestic Servant.
2 CONT Mary Chapman, Mother.
2 CONT 1908, March 16th. At Inverurie.
2 CONT Leslie Alexander, Registrar.
2 CONT
2 CONT
2 CONT Possible natural father is Alexander Miller of Drum Castle, Drumoak, Aberdeenshire. Earl's mother was a domestic servant at the castle and the name Alexander Miller is used by the Irvine family who lived there at that time. As an aside, his Gt. Aunt Marj
2 CONC orie Chapman was a nursery nurse in the royal household and had a pension from Queen Victoria ( who held her in high esteem, according to a newspaper clip, found at the time of his Aunt also Marjory's death) and was honoured at the time of her death by Q
2 CONC ueen Ena of Spain, who laterly had been her employer.
1 SOUR CERTIFICATES, B.C.,M.C.,D.C.
1 RFN 19254694
1 FAMS @7@
1 FAMC @113@
0 @19254695@ INDI
1 NAME Annie* Watson /Naismith/
1 SEX F
1 BIRT
2 DATE 2 SEP 1913
2 PLAC Lochmaben, Dumfries & Galloway, Scotland
1 DEAT
2 DATE 2005
2 PLAC INVERURIE Aberdeenshire Scotland
1 BAPM
2 DATE 16 OCT 1913
2 PLAC Lochmaben
1 OCCU
2 PLAC Clerkess(retired)
1 NOTE Statutory Birth Search, 840/00 0037:
2 CONT Annie Watson Naismith, Female, Lochmaben /Dumfries:
1 SOUR CERTIFICATES, SP Search
1 RFN 19254695
1 FAMS @7@
1 FAMC @114@
0 TRLR