Hi,

Right I've tried my best to get to grips with the whole regex for this but I just can't seem to wrap my head round it.

All I want to achieve is do a search for some text and delete it including the trailing space.

I think I need to use preg_replace and not trim because I am including the \n newline in the regex.

//content is an external text file i'll give you an example of contents as the variable ok.

$contents = "
[INDENT]1 BLIP Some text here
1 BLIP Some text here
1 BLIP Some text here
2 BLAH continued text here
1 BLIP Some text here
1 BLIP Some text here
2 BLAH continued text here
1 BLIP Some text here
2 BLAH continued text here
1 BLIP Some text here"[/INDENT]

$stripped = preg_replace("/\n2 BLAH /","", $contents);
echo "$stripped";

I want to delete the "2 BLAH " but so far I've only been able to delete "2 BLAH"

Please help and thanks in advance.

    Could it possibly be a tab or other white-space character instead of a word space? (The "\s" escape sequence in your pattern can be used for any white-space character.)

      NogDog;10925073 wrote:

      Could it possibly be a tab or other white-space character instead of a word space? (The "\s" escape sequence in your pattern can be used for any white-space character.)

      Yes it could be a tab or other whitespace character, I've tried using the \s

      $stripped = preg_replace("/\n2 BLAH\s/","", $contents);

      and still get the space, I'm probably using the wrong regex syntax and basically I'm just going round in circles trying all the different expressions to no avail.

        The pattern /\n2 BLAH / would not match "\n 2 BLAH continued text here" because the newline is followed by whitespace, whereas the pattern matches a newline followed by a '2'. Perhaps you should try /\n\s*2 BLAH /. By the way, use single quotes to quote your regex patterns so as to avoid extra escaping.

          laserlight;10925083 wrote:

          The pattern /\n2 BLAH / would not match "\n 2 BLAH continued text here" because the newline is followed by whitespace, whereas the pattern matches a newline followed by a '2'. Perhaps you should try /\n\s*2 BLAH /. By the way, use single quotes to quote your regex patterns so as to avoid extra escaping.

          Right I'm not sure how to edit the first post. It should actually look like this:-

          $contents = "
          1 BLIP Some text here
          1 BLIP Some text here
          1 BLIP Some text here
          2 BLAH continued text here
          1 BLIP Some text here
          1 BLIP Some text here
          2 BLAH continued text here
          1 BLIP Some text here
          2 BLAH continued text here
          1 BLIP Some text here"
          
          $stripped = preg_replace("/\n2 BLAH /","", $contents);
          echo "$stripped";
          

          There is no space after the \n new line. Single quotes, noted.

            Right. You need to know what exactly you are trying to match. With your given example, once I fix the typographical error, I see no problem at all. In fact, that there is a typographical error means that you did not actually test out the code that you posted.

              laserlight;10925088 wrote:

              Right. You need to know what exactly you are trying to match. With your given example, once I fix the typographical error, I see no problem at all. In fact, that there is a typographical error means that you did not actually test out the code that you posted.

              Ok i done what you said and tried the code on it's own and yup you're right it works, problem is though the code doens't work with my whole code if you know what i mean. I'm going to need to search through it all.

              I can't uderstand why it's working in the small code and even a slightly bigger bit from when i call it from out the array. I'll get on it.

                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
                  Write a Reply...