Jump to content

Parsing File and Printing Only Certain Lines


TripleDES

Recommended Posts

I would do it with a regular expression.... maybe something like:

 

//edit: this code doesnt work... ;p

 

The output would be the text without the begin x and end y lines.

 

(P.S.  I didn't test that, so if it doesn't work, sorry ;p)

Link to comment
Share on other sites

Ahh found a regular expression that works!

 

<?php
$content = "-- Begin X --
We are constantly
trying to improve
phpfreaks and these forums
-- End Y --";

$pattern = "/-- Begin ([a-z]+) --(.*)-- End ([a-z]+) --/si";
//$pattern = "/\-\- Begin ([a-z]+) \-\-(.*) \-\- End ([a-z]+) \-\-/si";
preg_match($pattern, $content, $matches);
//echo $matches[1];
echo '<pre>';
print_r($matches);
echo '</pre>';
/*
Output:

Array
(
    [0] => -- Begin X --
We are constantly
trying to improve
phpfreaks and these forums
-- End Y --
    [1] => X
    [2] => 
We are constantly
trying to improve
phpfreaks and these forums

    [3] => Y
)

*/
?>

Link to comment
Share on other sites

$pattern = "/-- Begin ([a-z]+) --(.*)-- End ([a-z]+) --/si";

 

Wow...that regex is too complicated for me to understand.  Please explain it to me so I can learn.

 

The file is not very big.  It's simply a mime encoded email that I'm trying to parse out the message body (text) so I can forward it via SMS.  If there's a better way to go about it, please enlighten me.

 

X = --mimepart_......

 

body...text...etc....

 

Y = ---

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.