Toon posts:

[REGEXP] code tussen 2 string ophalen

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik dacht ik maak snel ff een regexp om een stuk code uit een bestand te halen. de code staat steeds tussen <!--begin--> en <!--end--> in dus ik dacht ik maak de regexp

"<!--begin-->(.*?)<!--end-->", maar dit werkt dus niet.

suggesties om dit in een PHP eregi() functie te gieten?

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 15-04 22:07

NMe

Quia Ego Sic Dico.

PHP:
1
2
3
4
5
6
$string = "<!--begin-->test<!--end-->
           <!--begin-->blaat<!--end-->";

preg_match_all("/<!--begin-->(.*?)<!--end-->/i", $string, $matches);

print_r($matches);

Werkt perfect hoor. :?

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Verwijderd

Topicstarter
ja maar de string begint of eindigt niet met die comment, die staat er ergens middenin

Verwijderd

Topicstarter
volgens mij doet ie het niet omdat de code tussen de <!--begin--> en <!--end--> HTML code is en dus vol staat met speciale characters.

  • sorted.bits
  • Registratie: Januari 2000
  • Laatst online: 10:39
Daar heeft het niets mee te maken geloof ik, want met .* selecteer je alles.

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 15-04 22:07

NMe

Quia Ego Sic Dico.

Nope, depl0y, met . match je elk karakter behalve newlines. :)

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
<pre>
<?

$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
           do eiusmod tempor incididunt ut labore et dolore magna aliqua.
           Ut enim ad minim veniam, quis nostrud exercitation ullamco
           laboris nisi ut aliquip ex ea commodo consequat. Duis aute
           irure dolor in reprehenderit in voluptate velit esse
           cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
           cupidatat non proident, sunt in culpa qui officia deserunt
           mollit anim id est laborum. <!--begin-->test<!--end-->
           Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
           do eiusmod tempor incididunt ut labore et dolore magna aliqua.
           Ut enim ad minim veniam, quis nostrud exercitation ullamco
           laboris nisi ut aliquip ex ea commodo consequat. Duis aute
           irure dolor in reprehenderit in voluptate velit esse
           cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
           cupidatat non proident, sunt in culpa qui officia deserunt
           mollit anim id est laborum.           
           <!--begin-->blaat<!--end-->
           <!--begin-->
           Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
           do eiusmod tempor incididunt ut labore et dolore magna aliqua.
           <!--end-->";

preg_match_all("/<!--begin-->([\s\S]*?)<!--end-->/im", $string, $matches);

print_r($matches);

?>
</pre>

Output:
Array
(
    [0] => Array
        (
            [0] => test
            [1] => blaat
            [2] => 
           Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
           do eiusmod tempor incididunt ut labore et dolore magna aliqua.
           
        )

    [1] => Array
        (
            [0] => test
            [1] => blaat
            [2] => 
           Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
           do eiusmod tempor incididunt ut labore et dolore magna aliqua.
           
        )

)

Werkt perfect. :P

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • Glimi
  • Registratie: Augustus 2000
  • Niet online

Glimi

Designer Drugs

(overleden)
-NMe- schreef op dinsdag 21 juni 2005 @ 14:30:
Nope, depl0y, met . match je elk karakter behalve newlines. :)
En de grap is dat er in HTML waarschijnlijk wel newlines staan ;) Probeer de m modifier es

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 15-04 22:07

NMe

Quia Ego Sic Dico.

Glimi schreef op dinsdag 21 juni 2005 @ 14:34:
[...]

En de grap is dat er in HTML waarschijnlijk wel newlines staan ;) Probeer de m modifier es
Die gebruik ik toch ook? ;) Bovenstaande code werkt voor zover ik kan zien prima.
.     Matches any single character except "\n". To match any character including the '\n', use a pattern such as '[\s\S].

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Verwijderd

Topicstarter
deze regexp doet het wel, alvast bedankt

PHP:
1
preg_match_all("/<!--begin-->([\s\S]*?)<!--end-->/i", $string, $matches);

  • Glimi
  • Registratie: Augustus 2000
  • Niet online

Glimi

Designer Drugs

(overleden)
-NMe- schreef op dinsdag 21 juni 2005 @ 14:38:
Die gebruik ik toch ook? ;) Bovenstaande code werkt voor zover ik kan zien prima.
* Glimi heeft hem de eerste keer gemist :) excuus

  • sorted.bits
  • Registratie: Januari 2000
  • Laatst online: 10:39
Weer wat geleerd, neem aan dat dit ook geldt voor normale reg-exp's ?

* sorted.bits vind perl reg-exp's geen normale reg-exps :P
Pagina: 1