help with regular expressions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

help with regular expressions

Post 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
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

Thanks, I’ll read it... but still if anyone can give me an example I'll appreciated
hokiecsgrad
Forum Newbie
Posts: 17
Joined: Fri Oct 22, 2004 2:55 pm

Post 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];
?>
Post Reply