PDA

View Full Version : split running sentences apart


Mindless
04-19-2006, 08:00 AM
hello. I am trying to put together a code to split words apart at the capital letters.

ex:
IAmRunningOnThisSentence
to
I Am Running On This Sentence

1) is this possible?

2) what would be the best way to go about this?

they are variables at first so if it is easier to go from variable to the end result go ahead.

if this sint possible, i will work my other coding around to avoid this problem.

thanks in advance,
-Mindless-

GJay
04-19-2006, 08:25 AM
//a word is a capital followed by zero or more lower-case letters
$pattern='#([A-Z]+[a-z]*)+#';
//match the patterns
preg_match_all($pattern,$string,$matches,PREG_SET_ORDER);
$newstring='';
foreach($matches as $word)
$newstring.=$word;
//$newstring contains the output

Untested, but should be close...

Mindless
04-19-2006, 08:42 AM
where does $mathes get set?
it just seems to come out of nowhere...

this is what ive got now then


$artist = $_GET['artist'];
$string = $artist;
//a word is a capital followed by zero or more lower-case letters
$pattern='#([A-Z]+[a-z]*)+#';
//match the patterns
preg_match_all($pattern,$string,$matches,PREG_SET_ORDER);
$newartist='';
foreach($matches as $word)
$newstring.=$word;
//$newstring contains the output
echo $newstring;



when it gets echoes, it just print this:
Array

GJay
04-19-2006, 11:05 AM
$matches is the name the matches from preg_match_all get put in to.
if you do a print_r of $matches it should be obvious what I've missed in the looping, or take a look at the manual page http://php.net/preg-match-all

Mindless
04-20-2006, 06:02 AM
$artist = $_GET['artist'];

$string = $artist;
//a word is a capital followed by zero or more lower-case letters
$pattern='#([A-Z]+[a-z]*)+#';
//match the patterns
preg_match_all($pattern,$string,$matches,PREG_SET_ORDER);

$newstring=$matches[0][0] . " " . $matches[0][1];

foreach($matches as $word)
$newstring.=$word;
//$newstring contains the output

print_r($newstring);


so far i am trying to seperate the words Linkin Park (LinkinPark)
this is what im getting so far:

LinkinPark ParkArray

i have tried several different way of arranging the parameters
(ie: [0][1] && [1][1] etc...) and i cannot seem to get it to print it correctly.

the current code is as close as i can get it.
the rest of the ways seem to just print the words 'Array'

am i getting close?

are there any other hints you can give me?

i wont ask for the actual code since you seem to be trying to make this a elarning experience, and so far it has been, i just cant seem to figure it out. arrays have always eluded me.

GJay
04-20-2006, 08:29 AM
I wasn't intentionally being so brief, I didn't have a PC with PHP on at the time do anything other than think+type :)


$artist = 'LinkinParkAreGreat';
$string = $artist;
//a word is a capital followed by zero or more lower-case letters
$pattern='#([A-Z][a-z]*)#';
//match the patterns
preg_match_all($pattern,$string,$matches);

print_r($matches);


There's a slight change to the Regexp, and the flag for preg_match_all is gone, and it outputs:

Array
(
[0] => Array
(
[0] => Linkin
[1] => Park
[2] => Are
[3] => Great
)

[1] => Array
(
[0] => Linkin
[1] => Park
[2] => Are
[3] => Great
)

)

Which means you can do:

$newstring='';
foreach($matches[0] as $word)
$newstring.=$word.' ';

(probably best to add a check that there were matches (check the count() of the array, and you might want to trim the trailing space off $newstring)

Mindless
04-20-2006, 10:29 AM
wow this really did help me out. the code is working beautifully.
thanks a bunch for showing me where i was going wrong and providing the code. i was sort of close lol.

trib4lmaniac
04-21-2006, 06:04 PM
It's actually one heck of a lot simpler than even that:
$words = preg_split('/\B(?=[A-Z])/', 'CamelCasedString');