Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 52
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Read text into variable

    All,
    I can find a string in a set of data by using the following code:
    PHP Code:
    if(strpos($data"Subject:")){
    echo 
    "Subject is found";

    Now that I can find it, I want to be able to read the rest of the line into a $subject variable. How can I do this?

    Thanks in advance.

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    62
    Thanks
    19
    Thanked 1 Time in 1 Post
    1) One way
    http://php.net/manual/en/function.substr.php
    With substr() you can get rest of the string from an index.

    2) Another way
    http://php.net/manual/en/function.explode.php
    use explode() to deliver the string into an array which has in first index the
    string Subject: and in another index the rest of the string. Then you can
    access the rest of the string with $arrayName[1] for example.

    These came up to my mind first

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $data sounds to me like its a multi-line string. So, use a pattern match for it instead:
    PHP Code:
    $matches = array();
    if (
    preg_match('/Subject:(.*)\n|$/'$data$matches))
    {
        print 
    $matches[1]; // That should be the rest.

    But I guess that all depends on what $data is.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    treeleaf20 (10-13-2009)

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    That worked perfect. Thanks.

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Another question, if I have to find another thing and then go to EOF how would I do that?

    I tried something like:
    PHP Code:
    $matches_string = array();
    if (
    preg_match('/X-Attachment-Id:(.*)EOF|$/'$data$matches_string))
    {
        
    $filename $matches_string[1];
        print 
    "<br><br>The non decoded string is: $filename";
        
    $filename base64_decode($filename);
        print 
    "<br><br>The decoded string is: $filename"// That should be the rest.

    But it didn't work. Thanks.

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    $filetype2 = array();
    if (preg_match('/CX-Attachment-Id.*)\n|.EOF', $data, $filetype2))
    {
    $filetypeuse2 = $filetype2[1];
    echo "The filetypeuser2 is $filetypeuse2";
    }

    I also tried this but get an error saying the EOF isn't an ending delimiter.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    In pcre (perl compatible regular expressions, the patterns used in preg), $ denotes end of content line. Using what is above will handle any entry that has the string provided, match the given criteria, and finish with either a new line or end of content.
    If you're using a file read (fopen with fread), you need to retrieve one line at a time in order to use the match, but can jump to the end of the file using fseek($fileHandle, 0, SEEK_END);. feof() is used to determine if the fread should continue, but I don't believe that PHP has a supported EOF constant.

    With what you are doing here, you can actually look at using a preg_match_all, with pattern of '/(.*):(.*)\n|$/mU' with PREG_SET_ORDER to capture all of you're combinations and do later comparisons. That would look like so:
    PHP Code:
    $matches = array();
    preg_match_all('/(.*):(.*)\n|$/mU'$input$matchesPREG_SET_ORDER); 
    Something along that lines (I can't do any testing atm).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I am using fread. Here is the rest of the code:
    PHP Code:
    fwrite($fp"RETR 1\n"32); 

    $octets fgets($fp64); // read the first line 
    $parts explode(' '$octets); // split the first line by spaces 
    $octets $parts[1]; // we want the data after the second space 
    settype($octets'int'); // make sure its a number 

    $bytesread 0// we havent read any bytes of the actual email yet 
    $data 'Email Message Follows: <br /><br />'
    while(
    $bytesread $octets){ 

      
    $new_data fgets($fp4096); // read the data 
      
    $data .= '<br />'.$new_data// add it onto the existing data 
      //$data .= $new_data; // add it onto the existing data 
      
    $byteslength strlen($new_data); 
      
    $bytesread += $byteslength


    fread($fp64); 

    echo 
    $data
    When I subsituted my $data variable inplace of the $input variable I got the following error:
    Fatal error: Only variables can be passed by reference in /home/content/94/5043494/html/emailtest.php on line 56

    Thanks.

  • #9
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Sorry that was my fault, I updated it to the following:
    PHP Code:
    $files = array(); 
    preg_match_all('/(.*):(.*)\n|$/mU'$data$filesPREG_SET_ORDER);
    echo 
    "The files are ".$files[3]; 
    I get the output:
    The files are Array

    Why wouldn't it display what is in $files[3]?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $files[3] is a collection of every thing that matches each other. I'm wondering if the set order would be better than pattern order or not...
    If you print_r($files[3]), you should get back an array containing all of the 'equals' parts (as in: Subject: THIS VALUE HERE IS OFFSET 3) for every line in the $data. Data of course is only useful if its representable as a string - a resource will fail, so there would be no purpose to seek to end of file. If you're still wanting to abort once the proper line is matched, just use fgets to get the line, use a standard preg_match, see if you got what you need, and seek to end. Assuming of course that the fgets is in a while loop that stops when feof is true.

    Also, you can use named subpatterns, check out the last example here: http://php.ca/manual/en/function.preg-match-all.php

    Edit:
    Y'know what, I think I lied on that one. The set order will be the third matches (ie: to, from subject, it will be the entire collection of subject), followed by the offsets within subject. Man, y'know its been too long since you've used patterns when :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #11
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Here is an example of the data:

    --0016e64617d6b55eb30475d6fb3e--
    --0016e64617d6b55ebb0475d6fb40
    Content-Type: image/jpeg; name="psu_logo.jpg"
    Content-Disposition: attachment; filename="psu_logo.jpg"
    Content-Transfer-Encoding: base64
    X-Attachment-Id: f_g0r43rin0

    /9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
    AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodp
    AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTMiBXaW5kb3dz
    ADIwMDg6MTI6MTEgMTE6Mzg6MzAAAAAAA6ABAAMAAAAB//8AAKACAAQAAAABAAABLKADAAQAAAAB
    AAAAyAAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAAAAEAAAEmASgAAwAAAAEA
    AgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABJ1AAAAAAAAAEgAAAABAAAASAAAAAH/2P/gABBKRklG
    AAECAABIAEgAAP/tAAxBZG9iZV9DTQAC/+4ADkFkb2JlAGSAAAAAAf/bAIQADAgICAkIDAkJDBEL

    For some reason, even in the example you provided I can't get the following data:
    /9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
    AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodp
    AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTMiBXaW5kb3dz
    ADIwMDg6MTI6MTEgMTE6Mzg6MzAAAAAAA6ABAAMAAAAB//8AAKACAAQAAAABAAABLKADAAQAAAAB
    AAAAyAAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAAAAEAAAEmASgAAwAAAAEA
    AgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABJ1AAAAAAAAAEgAAAABAAAASAAAAAH/2P/gABBKRklG
    AAECAABIAEgAAP/tAAxBZG9iZV9DTQAC/+4ADkFkb2JlAGSAAAAAAf/bAIQADAgICAkIDAkJDBEL

    I can get the rest of it however. Any ideas?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That doesn't match the pattern.
    What happens if you use this:
    Code:
    '/(.*):(.*)/'
    in conjuntion with preg_match_all? Does that give everything associated with the X-Attachment-Id? If not, pattern matching prbbably isn't what you want. That inline content doesn't appear to be base64 btw.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I added the following to display the output:
    PHP Code:
    $files = array(); 
    preg_match_all('/(.*):(.*)/'$data$filesPREG_SET_ORDER);
    foreach (
    $files as $val) {
        echo 
    "<br>matched: " $val[0] . "\n";

    It does give me the attachment id:
    X-Attachment-Id: f_g0r43rin0

    However still doesn't give me anything with the encoded file. I've been told that everything after the X-Attachment-Id: f_g0r43rin0 is the file that I sent to my POP email account and then it got encode to base64. Is there anyway not to encode the attachment?

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Try two more potential changes to the pattern, these just involve modifiers:
    PHP Code:
    '/(.*):(.*)/s'

    // And if that doesn't work, try this:
    '/(.*):(.*)/sm' 
    I don't think the multi-line will matter, methinks that the dotall will suffice.

    As for you're question, yes PHP has a built in base64 encoder and decoder (of course, you can always remap if necessary). The problem is, it doesn't appear as base64. Its been awhile since I've encoded anything, but I'm pretty certain that base64 identifies itself by ending with ==.
    Also, if you add more to you're pattern, you may be able to capture it with a third pattern match using: (.*)?. I don't know if I'd recommend that though, since it may start getting greedy after that, so I'd go with splitting the results after with an explode.

    Try that code out post back - sadly I'm at work so I can't test anything out for you.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #15
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    That works great, I did the following:
    PHP Code:
    $filetype2 = array();
    if (
    preg_match('/(X-Attachment-Id:)(.*)/s'$data$filetype2))
    {
        
    $filetypeuse3 $filetype2[2];
        print 
    "<br><br>The subject is: $filetypeuse3"// That should be the rest.    

    When this gets output it gives me:
    The subject is: f_g0r43rin0

    /9j/4AAQSkZJRgABAgEASABIAAD/4ROrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA
    AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodp
    AAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTMiBXaW5kb3dz
    ADIwMDg6MTI6MTEgMTE6Mzg6MzAAAAAAA6ABAAMAAAAB//8AAKACAAQAAAABAAABLKADAAQAAAAB
    AAAAyAAAAAAAAAAGAQMAAwAAAAEABgAAARoABQAAAAEAAAEeARsABQAAAAEAAAEmASgAAwAAAAEA
    AgAAAgEABAAAAAEAAAEuAgIABAAAAAEAABJ1AAAAAAAAAEgAAAABAAAASAAAAAH/2P/gABBKRklG

    Is there anyway I can get rid of the f_g0r43rin0 followed by the two <br><br>?

    By the way, I only copied some of the output and the encoding. It's huge so I didn't want to take up the whole space. There are no == but I think it's probably encoded correctly.


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •