Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Check if there is a uppercase letter in the middle of a string
Old 06-25-2006, 05:21 PM Check if there is a uppercase letter in the middle of a string
Spin's Avatar
Super Talker

Posts: 113
Trades: 0
Is it possible? Im stuck here..

E.g.

check("String") -> false
check("StRing") -> true

And if its possible, how would you split the word in two? (St Ring)
Spin is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-25-2006, 06:20 PM Re: Check if there is a uppercase letter in the middle of a string
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
To detect them, you could do something like:

PHP Code:
function check($string){
  if(
ucfirst(strtolower($string))==ucfirst($string))
    return 
false;
  else
    return 
true;
}

check('StrIng'); //true
check('String'); //false 
I don't relly know how to solve your second problem, but it may be possible using regular expressions.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Old 06-25-2006, 09:56 PM Re: Check if there is a uppercase letter in the middle of a string
Average Talker

Posts: 15
Name: Jeffery Mandrake
Location: San Diego
Trades: 0
Quote:
Originally Posted by Spin
Is it possible? Im stuck here..

E.g.

check("String") -> false
check("StRing") -> true

And if its possible, how would you split the word in two? (St Ring)
---

I think this could be solved like this:

- parse each character in the string to see if it's upper case
- each upper case char's position is pushed into an array
- truncate the string according to the elements in the upper case char positions array -- eg. substr('abcdef', 0, 8); // abcdef
- use ucase() for each chunk and add a space...

I think you could also do this with a regular expression, but I'm not a reg exp buff.

Let me know if you need more details.
-Jeffery
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

~~~~~~~~~~~~~~~~~~~

Please login or register to view this content. Registration is FREE
cybernewsmaster is offline
Reply With Quote
View Public Profile Visit cybernewsmaster's homepage!
 
Old 06-25-2006, 11:22 PM Using str_replace() and preg_replace() to parse your string (PHP 3, PHP 4, or 5)
Average Talker

Posts: 15
Name: Jeffery Mandrake
Location: San Diego
Trades: 0
Quote:
Originally Posted by cybernewsmaster
---

I think this could be solved like this:
...
I think you could also do this with a regular expression, but I'm not a reg exp buff....
-Jeffery

OK. Forget about what I posted earlier.
I thought I had written something a while ago that addressed your issue... After a little fiddling, I came up with this solution:


function parseString($tempStr){
$search = array ("A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z");
foreach($search as $capChar){
$tempStr = str_replace($capChar, " ".$capChar, $tempStr);
}
//get rid of multiple spaces
$tempStr = preg_replace("'([ ])[\s]+'", "\\1", $tempStr);
return trim($tempStr);//get rid of extra white space
}

$myStr = "JustAnotherBeautifulDay InTheNeighborhood";
echo $myStr; // JustAnotherBeautifulDay InTheNeighborhood
echo "<br>";
echo parseString($myStr);// Just Another Beautiful Day In The Neighborhood

--
I tested it on my own server and it worked. See the output in the comments.
-Jeffery ~ BuffaloSoft - Cutting Edge Technologies
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

~~~~~~~~~~~~~~~~~~~

Please login or register to view this content. Registration is FREE
cybernewsmaster is offline
Reply With Quote
View Public Profile Visit cybernewsmaster's homepage!
 
Old 06-26-2006, 12:42 AM Re: Check if there is a uppercase letter in the middle of a string
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
Try this function:

PHP Code:

function isUpMid$str ){
        
$middle strlen$str ) % floorstrlen$str ) / ) : floorstrlen$str ) / ) - 1;
        return ( 
$str$middle } >= 'A' && $str$middle } <= 'Z' );
    } 
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 06-26-2006, 12:54 AM Re: Check if there is a uppercase letter in the middle of a string
Average Talker

Posts: 15
Name: Jeffery Mandrake
Location: San Diego
Trades: 0
Quote:
Originally Posted by ChancesAre
Try this function:

PHP Code:
 
function isUpMid$str ){
$middle strlen$str ) % floorstrlen$str ) / ) : floorstrlen$str ) / ) - 1;
return ( 
$str$middle } >= 'A' && $str$middle } <= 'Z' );

But to do what he wants: "...And if its possible, how would you split the word in two? (St Ring)"
you'll have to do a bit more work.
I thought the end result would matter more than just building a function that finds an upper case char. So I added a few more things he'd probably want to do as well, like removing multiple spaces, and chunking up the string into individual words (even if more than 2).
Not bad though. I haven't used floor() in a while.

-J
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

~~~~~~~~~~~~~~~~~~~

Please login or register to view this content. Registration is FREE

Last edited by cybernewsmaster; 06-26-2006 at 01:20 AM..
cybernewsmaster is offline
Reply With Quote
View Public Profile Visit cybernewsmaster's homepage!
 
Old 06-26-2006, 06:31 AM Re: Check if there is a uppercase letter in the middle of a string
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
Quote:
Originally Posted by cybernewsmaster
But to do what he wants: "...And if its possible, how would you split the word in two? (St Ring)"
you'll have to do a bit more work.
I thought the end result would matter more than just building a function that finds an upper case char. So I added a few more things he'd probably want to do as well, like removing multiple spaces, and chunking up the string into individual words (even if more than 2).
Not bad though. I haven't used floor() in a while.

-J
Oh i missed that...

I suggest use substr to split the words, since you already have the middle index store in $middle, you can use this variable as parameter for substr...
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 06-26-2006, 01:46 PM Re: Check if there is a uppercase letter in the middle of a string
Spin's Avatar
Super Talker

Posts: 113
Trades: 0
Thanks a lot guys for your great help.

I'll check it out when I get home.
Spin is offline
Reply With Quote
View Public Profile
 
Old 06-26-2006, 02:29 PM Re: Check if there is a uppercase letter in the middle of a string
Extreme Talker

Posts: 160
Trades: 0
$mystring = "There'sAlwaysMoreThanOneWayToDoIt!";

$mynewstring = preg_replace("/([^A-Z]*)([A-Z])([^A-Z]*)/","$1 $2$3",$mystring);
ElectricSheep is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Check if there is a uppercase letter in the middle of a string
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29485 seconds with 12 queries