Return string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Return string

Post by treesa »

Assume i have this string:
"hello";
if i give the number 2, the character 'l" has to be displayed.
i.e given the position, the charcter has to be displayed.how do i fetch the character of the string?



thanks
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Return string

Post by Oren »

Hmm... :?: not sure what you mean, but maybe this will help you:

Code: Select all

$str = 'hello';
$char = $str[1]; // $char == 'e'
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Return string

Post by josh »

also look at substr(). or while youre at it the rest of the string functions PHP has
http://us.php.net/manual/en/ref.strings.php
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Re: Return string

Post by treesa »

assume im having a string......
str="This is a string."
Using random function,need to capitalize it.
eg: THis Is a STring
The input string can vary each time
shaneiadt
Forum Newbie
Posts: 10
Joined: Sat Mar 15, 2008 9:26 pm

Re: Return string

Post by shaneiadt »

Just get the length of the string using the count() function, loop through the characters in the string and maybe have a rand() function to random between 1 and 0 and if it's 1 Capitalize and if 0 leave it the way it is.

There you go :)

I hope that helped!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Return string

Post by jayshields »

Are we doing your homework? The first question about characters in strings can be achieved using

Code: Select all

$str = 'Hello';
echo $str{2};
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Return string

Post by Darkzaelus »

treesa wrote:assume im having a string......
str="This is a string."
Using random function,need to capitalize it.
eg: THis Is a STring
The input string can vary each time

Code: Select all

 
<?php
$str='Hello';
$str2='';
for($i=0;$i<sizeof($str);$i++)
    $str2.=mt_rand(0,1)?strtoupper($str[$i]):strtolower($str[$i]);
return $str2;
//Will return a randomly capitalised word(s)
?>
 
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Re: Return string

Post by treesa »

thanks,
the above works only if we give strlen($str) instead of sizeof ($str).
also what does this line
$str2 .= mt_rand(0,1)?ucfirst($str[$i]):strtolower($str[$i]); indicate?
does it mean $str2 = $str2. mt_rand(0,1)?ucfirst($str[$i]):strtolower($str[$i]) ?

thanks
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Re: Return string

Post by treesa »

also please explain this mt_rand(0,1)?ucfirst($str[$i]):strtolower($str[$i]);
is this similar to if statement?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Return string

Post by josh »

Yep,
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
http://us3.php.net/language.operators.comparison
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Return string

Post by Darkzaelus »

Basically it's shorthand for writing:

Code: Select all

 
$temp=mt_rand(0,1);
    if($temp==0)
        $str2.=strtoupper($str[$i]);
    else
        $str2.=strtolower($str[$i]);
 
Sorry bout that :P

Cheers, Darkzaelus
Post Reply