<p>I'm writing a PHP script that involves scraping web pages. Currently, the script analyzes the page line by line, but it breaks if there is a tag that spans multiple lines, like </p>

<pre><code>&lt;img src="example.jpg"
alt="example"&gt;
</code></pre>

<p>If worse comes to worst, I could possibly preprocess the page by removing all line breaks, then re-inserting them at the closest <code>&gt;</code>, but this seems like a kludge.</p>

<p>Ideally, I'd be able to detect a tag that spans lines, conjoin only those to lines, and continue processing.<br>
So what's the best method to detect this?</p>

Well, this doesn't answer the question and is more of an opinion, but...
I think that the best scraping strategy (and consequently, to eliminate this problem) is not to analyze an HTML line by line, which is unnatural to HTML, but to analyze it by its natural delimiter: <> pairs.
There will be two types of course:
Tag elements that are immediately closed, e.g., < br />
Tag elements that need a separate closing tag, e.g., < p > text < /p >
You can immediately see the advantage of using this strategy in the case of paragraph(p) tags: It will be easier to parse mutiline paragraphs instead of having to track where the closing tag is.