[php] Alle text uitlezen behalve de HTML

Pagina: 1
Acties:

  • BierPul
  • Registratie: Juni 2001
  • Laatst online: 29-08 23:13

BierPul

2 koffie graag

Topicstarter
Ik heb het volgende geprogd om alle HTML files die in de mappen en submappen staan uit te lezen om op keyword te gaan indexeren.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$root = "$DOCUMENT_ROOT/search";

function leesdir($root) {
    if ($dir = @opendir($root)) {
        while (($file = readdir($dir)) !== false) {
            if($file !== "." and $file !== "..") {
                $path = $root . '/' . $file;        
                 if(is_dir( $path )) { 
                    leesdir($path);
                } else {
                    $extension = substr($file, -4);
                        if($extension == "html" or $extension == ".htm") {                          
                            $rows = file($path);
                                foreach ( $rows as $r ) {
                                    if(preg_match_all( '/<[^>]*>([^<]*)/', $r, $m )) {
                                        print_r($m);
                                    }
                                }
                        }
                }
            }
        }
    }
}

leesdir($root);


Alleen komt het er na mn regex zo uit dat ik alleen de waardes krijg die tussen >en <

staat :(

Ik wil alleen de text en niets wat tussen <> staat :)

Dus alles behalve de HTML :)

* BierPul is niet zo'n regex koning 8)7

Ja man


Verwijderd

strip_tags();

Dan ben je netjes af van alle html code...

  • narotic
  • Registratie: Maart 2002
  • Laatst online: 02-11-2021
Is daar niet een standaard methode voor zodat je niet met regex hoeft te kloten?

string strip_tags ( string str [, string allowable_tags])

http://www.php.net/manual/en/function.strip-tags.php voor info


en is t niet netter / handiger om de gehele content van de files als een string op te slaan en daar dan strip_tags over te doen?

- = Step Into The Pit | Industrial Strength = -


  • BierPul
  • Registratie: Juni 2001
  • Laatst online: 29-08 23:13

BierPul

2 koffie graag

Topicstarter
leuker kunnen we het niet maken wel makkelijker :)

Ja man


  • Ericston
  • Registratie: Maart 2001
  • Laatst online: 05-08 18:36
http://php.nederland.net/manual/nl/function.preg-replace.php
En anders die regex die hier bij voorbeeld 3 staat.

  • RSD
  • Registratie: Maart 2001
  • Laatst online: 08-02-2017

RSD

Snoopy van sourceforge heeft ook zoiets, is best een handige class, kun je het adres opgeven van de url en snoopy genereert alle text op die pagina of alle links of ??? wat dan ook.. een aanradertje zeg maar... maar niet handig om op je site te gebruiken, wel handig om leuke dingen mee te doen :)

Verwijderd

Mooie functie ja.. maar wat d8 je van tags als bbabalb in <script language = "javascript>bbabalb</script> gebruikt word

Verwijderd

Ik dacht ook al, wat voor vraag wordt er nu op het forum gesteld :+

strip_tags() is inderdaad te gebruiken, en er staat ook een hele mooie regex in de manual :)

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Example 635. Convert HTML to text

// $document should contain an HTML document.
// This will remove HTML tags, javascript sections
// and white space. It will also convert some
// common HTML entities to their text equivalent.

$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                "'<[\/\!]*?[^<>]*?>'si",           // Strip out html tags
                "'([\r\n])[\s]+'",                 // Strip out white space
                "'&(quot|#34);'i",                 // Replace html entities
                "'&(amp|#38);'i",
                "'&(lt|#60);'i",
                "'&(gt|#62);'i",
                "'&(nbsp|#160);'i",
                "'&(iexcl|#161);'i",
                "'&(cent|#162);'i",
                "'&(pound|#163);'i",
                "'&(copy|#169);'i",
                "'&#(\d+);'e");                    // evaluate as php

$replace = array ("",
                 "",
                 "\\1",
                 "\"",
                 "&",
                 "<",
                 ">",
                 " ",
                 chr(161),
                 chr(162),
                 chr(163),
                 chr(169),
                 "chr(\\1)");

$text = preg_replace ($search, $replace, $document);

  • narotic
  • Registratie: Maart 2002
  • Laatst online: 02-11-2021
Verwijderd schreef op 26 oktober 2002 @ 11:25:
Mooie functie ja.. maar wat d8 je van tags als bbabalb in <script language = "javascript>bbabalb</script> gebruikt word
FF voor de info: language is Deprecated in de HTML 4.01, je moet nu 'type=...' gebruiken. FYI

- = Step Into The Pit | Industrial Strength = -


  • SWINX
  • Registratie: Juni 2001
  • Laatst online: 02-06 23:18
en ook ff ter informatie: text/javascript moet het dan dus worden :)

Mannen komen van Mars Tweakers, vrouwen van Venus Bokt


  • narotic
  • Registratie: Maart 2002
  • Laatst online: 02-11-2021
als je javascript wilt gebruiken wel ja, anders niet he :D

FF serieus, meer info hierover valt hier te vinden: http://www.w3.org/TR/1999/REC-html401-19991224/

- = Step Into The Pit | Industrial Strength = -

Pagina: 1