First question: [man]substr[/man].
Second question: what do you mean "only for PDF files"? All PDF files start with those bytes. What other file types do is their own concern. One thing's for sure - if it starts with %PDF- then it can't be a JPEG, a PNG, an executable, an MP3, an email, a zip archive, a Word document, an HTML document, .....