Split a string - or can you?

Links for php scripts

Moderators: macek, egami, gesf

ddrake1984
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Mar 21, 2012 7:51 pm

Split a string - or can you?

Postby ddrake1984 » Wed Mar 21, 2012 7:53 pm

I need to split a string, but in a very unusual way.

$string1 = "HelloWorld"
$string2 = "AlphaBravo"
$string3 = "BreakMeUp"

I need the following split at each upppercase letter for this output

Hello World
Alpha Bravo
Break Me Up

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Split a string - or can you?

Postby egami » Thu Mar 22, 2012 4:05 am

Investigate strpos()

php.net/strpos

ddrake1984
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Mar 21, 2012 7:51 pm

Re: Split a string - or can you?

Postby ddrake1984 » Thu Mar 22, 2012 2:05 pm

I received the assistance I needed from phpfreaks.com

$string = 'ThisText';

$newstring = '';
foreach( str_split( $string ) AS $letter ) {
$newstring .= ( ctype_upper( $letter ) ? ' ' : '' ) . $letter;
}
$string = trim( $newstring );


Return to “PHP Scripts”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest