find and remove chunk of a string, (more than replace())

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

find and remove chunk of a string, (more than replace())

Post by shawngoldw »

Suppose I have this entire text contained in a single string variable:
[text]
--=_b6a990e35093cac7efaddd6539ff8b60
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=us-ascii;
charset=us-ascii

test1
test2
test3
[/text]

Some of you may recognize this as part of the body of MIME email, that's because it is! Anyways, I need a script which can check the Content-Type of this block of text. If the Content-Type is text/plain I need the script to then delete everything in the string up until test1.

Proceeding test1 should be \r\n\r\n so I can do a replace(substr(STRING, 0, strpos(STRING, '\r\n\r\n')), '') or some variation like that (there's probably a syntax error in there). I'm not sure how to do the first part of traversing the string and checking the Content-Type.

Any advice?

Thanks,
Shawn
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: find and remove chunk of a string, (more than replace())

Post by requinix »

Easiest way would probably be with regular expressions.

Code: Select all

if (!preg_match('#Content-Type:\s*([^/]+/[^;\r\n]+)\r\n#i', $text, $matches) || $matches[1] == "text/plain") {
    // whatever
}
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: find and remove chunk of a string, (more than replace())

Post by shawngoldw »

aahhhh yes.... regular expressions..... those things that I need to familiarize myself with.... :D

Thank you
Post Reply