Page 1 of 1

help with regular expressions

Posted: Thu Oct 28, 2004 3:59 pm
by paquin1
I have read the php manual and some other websites, but I’m still not getting it very well, so I'm asking if anyone first knows of a good website that will explain it in simple terms and with lots of examples (since I learn by examples).
And second if anyone can give me a sample of how to do a preg_match () for getting all the text between two words in a document.
So the idea is something like this:

Code: Select all

<?php
preg_match("/^(initialword) to (endword)$/", $file, $matches);
?>
thanks

Posted: Thu Oct 28, 2004 4:04 pm
by Draco_03

Posted: Thu Oct 28, 2004 4:28 pm
by paquin1
Thanks, I’ll read it... but still if anyone can give me an example I'll appreciated

Posted: Fri Oct 29, 2004 10:06 am
by hokiecsgrad
To get the text between an initial word and the end word, assuming you know both:

Code: Select all

<?php
  $initialWord = 'init'; 
  $endWord = 'end';
  $find = '/^' . $initialWord . ' (.*) ' . $endWord . '$/';

  $search_text = 'init I''m the text between the init and end end';

  preg_match( $find, $search_text, $matches );
  echo $matches[1];
?>