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!
Probably yet another regular expressions question
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.feyd wrote:Code: Select all
preg_match_all('#(їA-Z]їa-z]+(\s+їA-Z]їa-z]+)*)#m', $text, $matches); var_export($matches);
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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',
),
)- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
You can simply do the following to get what you want.
Code: Select all
$matches = $matches[0];- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Just another quick point on a side note: (?:pattern) Doesn't get stored, just groupedfeyd 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];