By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

how to find middle character of string?

P: 1
if my string is "welcome" then i want output as its middle character is "c"
then tell me how to write program for this in php??
Oct 5 '11 #1
Share this Question
Share on Google+
1 Reply

P: 638
strlen() gives you the size of the string take the median of that size and the resultant-1 will be the index of the string you need to show , here give it a try
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     $string="welcome";
  3.     echo "string is ".$string."<br />";
  4.     $length=strlen($string);
  5.     echo "total length is ".$length."<br/>";
  6.     $length=round($length/2);
  7.     echo "middle string will be no ".$length." / the index number 3 which will be the fourh item in that string <br/>";
  8.     echo $string[$length-1];
  9. ?>
Oct 5 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.