PHP preg_replaceを使って欠落しているHTMLタグを閉じるための簡単な関数を書こうとしています。否定的な見方を使ったPHP正規表現のヘルプ
私はそれが比較的簡単だろうと思っていましたが、何らかの理由でそれがされていませんでした。
私は基本的にやろうとしていることは、次の行に近い欠けているタグです:
<tr>
<th class="ProfileIndent0">
<p>Global pharmaceuticals</p>
<td>197.2</td>
<td>94</td>
</tr>
私が取ってきたアプローチであるTDタグを開く見つけることが背景にある負の外観を使用することですタグが正しく開かれていない場合例えば
:私は無駄に正規表現パターン無数の異なる方法を書いた
$text = preg_replace('!<th(\s\S*){0,1}?>(.*)((?<!<\/th>)[\s]*<td>)!U','<th$1>$2</th>',$text);
。問題は、先に見つからなかった1つのオープンtdだけでは一致しないように見えますが、オープンtdタグのいくつかに一致するようです。ここで
は、完全な入力テキストです:
<CO_TEXT text_type_id="6">
<TEXT_DATA><![CDATA[<table class="ProfileChart"> <tr> <th class="TableHead" colspan="21">2008 Sales</th> </tr>
<tr> <th class="ProfileIndent0"></th> <th class="ProfileHead">$ mil.</th> <th class="ProfileHead">% of total</th> </tr>
<tr> <th class="ProfileIndent0"> <p>Global pharmaceuticals</p> <td>197.2</td> <td>94</td> </tr>
<tr> <th class="ProfileIndent0">Impax pharmaceuticals</th> <td>12.9</td> <td>6</td> </tr>
<tr> <th class="ProfileTotal">Total</th> <td class="ProfileDataTotal">210.1</td> <td class="ProfileDataTotal">100</td> </tr> </table><h3>Selected Generic Products</h3><ul class="prodoplist"><li>Anagrelide hydrochloride (generic Agrylin, thrombocytosis)</li><li>Bupropion hydr ochloride (generic Wellbutrin SR, depression)</li><li>Colestipol hydrochloride (generic Colestid, high cholesterol)</li><li>Dantrolene sodium (generic Dantrium, spasticity)</li><li>Metformin Hcl (generic Glucophage XR, diabetes)</li><li>Nadolol/Bendroflumethiazide (generic Corzide, hypertension)</li
><li>Oxybutynin chloride (generic Ditropan XL, urinary incontinence, with Teva)</li><li>Oxycodone hydrochloride (generic OxyContin controlled release, pain)</li><li>Pilocarpine hydrochlorine (generic Salagen, dry mouth caused by radiation therapy)</li></ul>]]></TEXT_DATA> </CO_TEXT>
は、私は認識していないよPHPで負のルック尻で起こって何かがあるのか、私はちょうど右のマッチングパターンにヒットしていませんか?
ご協力いただければ幸いです。
おかげで、 ジョン
こんにちは! (申し訳ありませんが、それはanwserではなく、ちょうど思考、多分それは他の方法があるかもしれないと思うのを助けるでしょう)あなたの正規表現を見て、私の心に来る唯一の事があります:regexesは "あなたがやろうとしていることのための正しいツール...正規表現を読むことはすでにかなり難しいですし、どんな混乱した疑似文も扱うことができなければならない混乱を想像しません-HTMLはそれを養うかもしれない... –
パスカル、はい - 私はあなたが言っていることを知っています。ここ数日間、私の頭を壁にぶつけた後、私はこの問題に対処するより良い方法があると考えています。特に、表示の終わりではなく、ソースで悪いHTMLを捕まえること。 – John