Probably yet another regular expressions question

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
Vodex
Forum Newbie
Posts: 3
Joined: Sun Mar 27, 2005 11:23 am

Probably yet another regular expressions question

Post by Vodex »

Hi. I'm knocking up a small app that will convert blog/forum posts to have links to Wikipedia. The idea is the user enters a string like

"A really good place to go is the London Eye - it's tall!"

And it picks out the capitalised phrase 'London Eye', turning into "http://en.wikipedia.org/wiki/London_Eye". But this means wrestling with regexps.

ereg & '[A-Z][a-z]+' finds a capitalised word ('London'). How can I find a string of capitalised words?

I'm not a regexp expert, and all tutorials seem to stop at 'validate an email address' - & not all flavours of regexps seem to be the same...

Whilst I'm at it, how can I find a string containing underscores, so a user could go 'UK_general_election,_2001' for non-capitalised entries?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_match_all('#(їA-Z]їa-z]+(\s+їA-Z]їa-z]+)*)#m', $text, $matches);

var_export($matches);
Vodex
Forum Newbie
Posts: 3
Joined: Sun Mar 27, 2005 11:23 am

Post by Vodex »

feyd wrote:

Code: Select all

preg_match_all('#(їA-Z]їa-z]+(\s+їA-Z]їa-z]+)*)#m', $text, $matches);

var_export($matches);
That didn't seem to do it but have got it fixed now, thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

odd.. it sure works for me.

Code: Select all

<?php

$text = 'A really good place to go is the London Eye - it\'s tall!';

preg_match_all('#(їA-Z]їa-z]+(\s+їA-Z]їa-z]+)*)#m', $text, $matches);
 
var_export($matches);

?>

Code: Select all

array (
  0 =>
  array (
    0 => 'London Eye',
  ),
  1 =>
  array (
    0 => 'London Eye',
  ),
  2 =>
  array (
    0 => ' Eye',
  ),
)
Vodex
Forum Newbie
Posts: 3
Joined: Sun Mar 27, 2005 11:23 am

Post by Vodex »

feyd wrote:odd.. it sure works for me.
Why do I want the phrase 'London Eye' twice, and then the word 'Eye' afterwards? I just want 'London Eye'.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's because of how the pattern is written. Each set of parens gets its own array, plus the entire match. That is the standard return from the regular expression matching functions..

You can simply do the following to get what you want. :|

Code: Select all

$matches = $matches[0];
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:that's because of how the pattern is written. Each set of parens gets its own array, plus the entire match. That is the standard return from the regular expression matching functions..

You can simply do the following to get what you want. :|

Code: Select all

$matches = $matches[0];
Just another quick point on a side note: (?:pattern) Doesn't get stored, just grouped :wink:
Post Reply