[ ljuba @ 16.01.2002. 17:12 ] @
Cao svima,

Imam sad jedan zadatak gde bih trebao html stranu da iseckam po paragrafima gde paragraf nije samo <p> i </p> nego i <td> i </td> tako da ako je stranica:

<td>test
<table>
<tr><td>test2 test3</td></tr>
<tr><td>test2<p>bla bla</p></td></tr>
</table>
</td>

rezultat bi trebao biti:

test
test2 test3
test2
bla bla

Znaci izmedju <p> i </p>, izmedju <td> i </td> ali i izmedju pocetka paragrafa <p> ili <td> i pocetka novog paragrafa <p> ili <td>. Znam da ima dosta rupa u ovakoj zamisli ali ako tako oce nema veze ... valjda znaju sta rade ...

Posto ja nisam strucnjak u tome pokusao sam da malo zaobilaznim putem dodjem do resenja. Jer ne znam kako bih ovo opisao jednim redom u RE. Smislio sam da sve gore pomenute tagove zamenim nekom 'cudnom' reci pa da onda isecam paragrafe izmedju tih reci. Napravio sam nesto ovako:

<?php /* Paragraph search */
// Take a source
$File = implode ("", file("page.html"));
// Remove all styles (not neccessary):
$File = eregi_replace ("<style(.*)</style>", "", $File);
// Remove all scripts (not neccessary):
$File = eregi_replace ("<script(.*)</script>", "", $File);
// Remove 'baggage' (not neccessary):
$File = eregi_replace ("<html(.*)<body>", "<html><body>", $File);
// Replace 'targets' with some unusual word:
$File = eregi_replace ("<td(.*)>", " <|P|> ", $File);
$File = eregi_replace ("</td(.*)>", " <|P|> ", $File);
$File = eregi_replace ("<p>", " <|P|> ", $File);
$File = eregi_replace ("</p>", " <|P|> ", $File);
// Take everything between <|P|> i <|P|>
if (preg_match_all("/<|P|>(.*)<|P|>/", $File, $matches)) {
for ($i=0; $i < count($matches[0]); $i++) {
echo "Paragraph" . $i . ": " . $matches[1][$i] . "<br>";
echo "--------------<br>";
}
}
?>

Ali ako napisem $matches bez prvog parametra dobijem u resenju samo rec array za svako 'nalazenje', ali ako napisem kao gore sto sam napisao, znaci sa tim parametrom, onda dobijem gresku:

Warning: Undefined offset: 1 in D:\Projekti\Par\index.php on line 18

Sta je problem, kako da ga preskocim i sta mislite o resenju?

Pozdrav i hvala

LJUBA
[ Divine @ 17.01.2002. 23:54 ] @
<?php
$document = implode("", file("test.html"));
$search = array("'<td[^>]*?>'", "'<tr[^>]*?>'", "'<\/tr[^>]*?>'", "'<table[^>]*?>'", "'<\/table[^>]*?>'");
$replace = array("", "", "", "", "");
$text = preg_replace($search, $replace, $document);

$text = eregi_replace("<p>", "\n", $text);
$text = eregi_replace("</p>", "\n", $text);
$text = eregi_replace("</td>", "\n", $text);
echo($text);
?>


evo ti rjesenje onog gore :)
preg_replace je funkcija iz Regular Expressions