You are here: Re: Detect Upper case characters in a string and separate them with a space « PHP Programming Language « IT news, forums, messages
Re: Detect Upper case characters in a string and separate them with a space

Posted by Alan on 03/19/07 12:28

"Toby A Inkster" <usenet200703@tobyinkster.co.uk> wrote in message
news:ms01d4-taf.ln1@ophelia.g5n.co.uk...
> zainally wrote:
>
>> I want to detect uppercase characters in a string and separate the two
>> words in it by a space. I know I can use preg_replace for it, but I am
>> not very good at regular expressions. Can someone help?
>
> You want something like this:
>
> $s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);
>
> You probably want to consider what sort of behaviour is "desirable" in
> edge cases like this:
>
> Print404ErrorPage
> GetHTTPRequest
> Jpeg2Gif
>
> The above regular expression will output:
>
> Print404 Error Page
> Get H T T P Request
> Jpeg2 Gif
>
> which might not be what you wanted!
>

Further to Toby's post:

$TestStr = Print404ErrorPage\nGetHTTPRequest\nJpeg2Gif\nJPEG";

$CapRegX = '/(\B[A-Z])(?=[a-z])|(?<=[a-z])([A-Z])/sm';
$RepStr = ' $1$2';
$OutStr = preg_replace($CapRegX,$RepStr,$TestStr);

produces:

Print404 Error Page
Get HTTP Request
Jpeg2 Gif
JPEG

HTH
--

Alan

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация