how to find middle character of string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sujata nipane
    New Member
    • Oct 2011
    • 1

    how to find middle character of string?

    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??
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    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
    [code=php]
    <?php
    $string="welcom e";
    echo "string is ".$string." <br />";
    $length=strlen( $string);
    echo "total length is ".$length." <br/>";
    $length=round($ length/2);
    echo "middle string will be no ".$length." / the index number 3 which will be the fourh item in that string <br/>";
    echo $string[$length-1];
    ?>

    [/code]

    Comment

    Working...