Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

regular expression strikes again

[es] :: PHP :: regular expression strikes again

[ Pregleda: 1347 | Odgovora: 9 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

dr ZiDoo
Web Developer
Banja Luka

Član broj: 189
Poruke: 1708
*.dialup.blic.net.

Jabber: ZiDoo@elitesecurity.org
ICQ: 299539598
Sajt: zidoo.geek.rs.ba


Profil

icon regular expression strikes again10.09.2005. u 17:52

Imam jedan problem i već 5 dana se patim :)=

HTML fajl:
Code:

...
<body>
 <table width="100%" cellspacing="1" cellpadding="1">
  ...
 </table>

 <table width="150" cellspacing="1" cellpadding="1">
  ...
 </table>

 ...

 <table width="150" cellspacing="1" cellpadding="1">
  ...
 </table>
...
</body<


E sada ka trenbam da pokupim sadržaj svih tabela koje imaju dužinu 150px ili ti svih tabela koje počinju: "<table width="150" cellspacing="1" cellpadding="1">".

Pokušo sam na milion načina i nijedan mi nije uspio, jednostavno uvjek ne dobijem dobar rezultat. Imali neki regexp guru, pošto meni očito ne ide...





[Ovu poruku je menjao dr ZiDoo dana 10.09.2005. u 18:53 GMT+1]
tu nema kašike....
10.09.2005. u 17:52 

noviKorisnik

Član broj: 13216
Poruke: 4513
*.dialup.neobee.net.



Profil

icon Re: regular expression strikes again10.09.2005. u 18:11
Mislio si širinu 150px?

Problem mogu da ti prave ugnježdene tabele (koliko znam, to obično tako ide, tabela u tabeli dok ne dosadi).

Probaću da se poigram, dok ne naiđe neki pravi guru...
10.09.2005. u 18:11 

noviKorisnik

Član broj: 13216
Poruke: 4513
*.dialup.neobee.net.



Profil

icon Re: regular expression strikes again10.09.2005. u 18:34
Evo nešto što će naravno da pukne na ugnježđenim tabelama... za početak
Code:
<?php
header ('Content-type: text/plain');
$filename = 'withtable.html';
$filecontent = file_get_contents ($filename);
$pattern = "/<table\b(.|\n)*\bwidth=\"150\"(.|\n)*>(.|\n)*<\/table>/";
preg_match_all ($pattern, $filecontent, $result);
print_r ($result);
?>
10.09.2005. u 18:34 

noviKorisnik

Član broj: 13216
Poruke: 4513
*.dialup.neobee.net.



Profil

icon Re: regular expression strikes again10.09.2005. u 18:51
Još gore, ulovi sve do poslednjeg "</table>" :-(
10.09.2005. u 18:51 

dr ZiDoo
Web Developer
Banja Luka

Član broj: 189
Poruke: 1708
*.teol.net.

Jabber: ZiDoo@elitesecurity.org
ICQ: 299539598
Sajt: zidoo.geek.rs.ba


Profil

icon Re: regular expression strikes again10.09.2005. u 21:44
Tak nesto slicno i ja dobijem... tj. dobio sam sve moguce kombinacije osim one prave :(
tu nema kašike....
10.09.2005. u 21:44 

japan

Član broj: 34328
Poruke: 370
*.smin.sezampro.yu.



Profil

icon Re: regular expression strikes again10.09.2005. u 21:56
Citat:
noviKorisnik: Još gore, ulovi sve do poslednjeg "</table>" :-(


možda ovo pomogne:
Citat:

By default, the quantifiers are "greedy", that is, they
match as much as possible (up to the maximum number of per-
mitted times), without causing the rest of the pattern to
fail. The classic example of where this gives problems is in
trying to match comments in C programs. These appear between
the sequences /* and */ and within the sequence, individual
* and / characters may appear. An attempt to match C com-
ments by applying the pattern

/\*.*\*/

to the string

/* first command */ not comment /* second comment */

fails, because it matches the entire string due to the
greediness of the .* item.

However, if a quantifier is followed by a question mark,
then it ceases to be greedy, and instead matches the minimum
number of times possible, so the pattern

/\*.*?\*/

10.09.2005. u 21:56 

flylord
Ilić Aleksandar
Freedomia (direktor)
Nis/Uzice

Tehnički konsultant
Član broj: 2954
Poruke: 3394
*.pat-pool.ni.sbb.co.yu.

Jabber: flylord@elitesecurity.org
ICQ: 4849714
Sajt: blog.itseminar.org


Profil

icon Re: regular expression strikes again10.09.2005. u 22:12
Moglo bi da pomogne da parsiras taj html preko xml-dom f-ja ... Pre toga odradi validaciju HTML-a preko html tidy-a. A onda preko dom-a mozes da pristupis svakom elementu
10.09.2005. u 22:12 

cabrilo
Dejan Cabrilo

Član broj: 4780
Poruke: 53
*.dsl.tpkaks.swbell.net.

ICQ: 7366253


Profil

icon Re: regular expression strikes again12.09.2005. u 22:39
Evo ti ga algoritam... Nece raditi ako imas jos neku tabelu u zeljenoj tabeli (tj. prekinuce se na prvom </table> posle pocetka citanja):

Code:

<?php
// Prvo ucitas celu stranu u $buffer
$buffer = file ("fajl");

// Gde da pocne da cita, gde da prekine
$startcode = "<table width=\"150\" cellspacing=\"1\" cellpadding=\"1\">";
$endcode = "</table>";

$entries = array ();
$counter = -1;
$read = FALSE;

// Cita samo kada je vec pronadjen pocetak, a nije pronadjen kraj. 
foreach ($buffer as $line) {
    if (strstr ($line, $endcode))
        $read = FALSE;
    if ($read)
        $entries[$counter] .= $line;    
    if (strstr ($line, $startcode)) {
           $read = TRUE;    
        $counter++;
    }
}
?>


Posle ovoga, sve ce ti se nalaziti u arrayu $entries... Dakle $entries[0] je iz prve tabele, $entries[1] iz druge itd.

Sad, ako bi zeleo da radi sa nested tabelama, trebalo bi da uradis proveru da li je otvoren jos jedan <table> kada je $read = TRUE, i onda ne prebacujes na $read = FALSE sve dok se to ne zatvori. Dodas jos jedan counter recimo, tako da za svaku novu tabelu koja se otvori, broj se poveca, i ne prekida da cita dok N tabela nije zatvoreno. Resetujes counter na 0 kada se sve zatvore...

I nije bilo regex-a :)
12.09.2005. u 22:39 

noviKorisnik

Član broj: 13216
Poruke: 4513
*.dialup.neobee.net.



Profil

icon Re: regular expression strikes again12.09.2005. u 23:14
Ova alternativa naravno radi samo u vrlo ograničenim slučajevima.

regexpi mi deluju komplikovani da obuhvate opšti slučaj definicije tabele, gde otvarajući tag može da se rastegne u više redova, gde potrebni atributi mogu da budu navedeni proizvoljnim redosledom, gde vrednosti mogu da se zadaju u jednostrukim ili dvostrukim navodnicima, ili bez njih.

Predlog o DOM parsiranju mi se veoma dopada, žao mi je što još nisam stigao da se time poigram u PHP-u, verujem da je zabavno kao i u drugim jezicima.
12.09.2005. u 23:14 

WinterMute
Slobodan Jovanović
Obrenovac

Član broj: 747
Poruke: 109
217.24.18.*

ICQ: 85025545
Sajt: www.zion.co.yu


Profil

icon Re: regular expression strikes again13.09.2005. u 08:31
Ovaj će da radi sa bilo kojom kombinacijom ova tri property-ja ali ne kapira ugnježdene tabele...

Code:
$regexp ="/<table\s+?(width=\"150\"\s+?|cellspacing=\"\d+?\"\s+?|cellpadding=\"\d+?\")(width=\"150\"\s+?|cellspacing=\"\d+?\"\s+?|cellpadding=\"\d+?\")(width=\"150\"\s+?|cellspacing=\"\d+?\"\s+?|cellpadding=\"\d+?\")>(.+?)<\/table>/si"


Baš me zanima da li samo koristeći regex uopšte može da se reši problem oko ugnježdavanja...?
We look into the net, and the net is growing wide and wider.
13.09.2005. u 08:31 

[es] :: PHP :: regular expression strikes again

[ Pregleda: 1347 | Odgovora: 9 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.