On Tue, 30 Apr 2002, Ed Lazor wrote:
> Pull everything except a specific word from a sentence.  For example, 
> pulling everything except the word run from "the water run was steep".

   $str = 'the water run was steep';
   print preg_replace('/(\s*water)/', '', $str);

> Pull all words from a string starting with a specific letter or 
> pattern.  For example, pulling all of the words starting with the letter r 
> from "run beep burp ran rin ron runt zoot zip pow"

   $str = 'run beep burp ran rin ron runt zoot zip pow';
   if (preg_match_all('/(\br\w+\b)/', $str, $matches))  
      print join(' ', $matches[1]);

> Pulling all words from a string excluding ones starting with a specific
> letter or pattern.  For example, pulling all of the words except ones
> starting with the letter r from "run beep burp ran rin ron runt zoot zip
> pow"

   $str = 'run beep burp ran rin ron runt zoot zip pow';
   if (preg_match_all('/([\b\W][^r]\w+\b)/', $str, $matches))
      print join(' ', $matches[1]);

> Pulling a word between two other words without having to know what the
> word is.  For example, pulling whatever word displays between "the" and
> "sky".  If the string was "the blue sky", the result would be the word
> blue.  If the string were "the green sky", the result would be the word
> green.

   $str = 'the green sky';                              
   if (preg_match('/the\s+(\S+?)\s+sky/', $str, $matches))
      print $matches[1];           

> I apologize in advance if these are really simple.  It's just that I'm
> new to regular expressions and reading all of the web page tutorials,
> manual pages, and mailing list archive messages has left me thinking I'm
> complicating something somewhere, because it really shouldn't be this
> hard.

Well, it's not trivial. Regular expressions is a whole complete language,
entirely separate from PHP, with a lot to learn. Practice enough, though,
and you'll start to see how you can do amazing things with it.

Also, I'd recommend using the Perl-style regex (preg_ rather than ereg_
functions) and reading 'man perlre' if it's installed on your system
(someone can probably suggest a web location for that text).

miguel


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to