Perl to PHP conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zishan
    New Member
    • Feb 2007
    • 2

    Perl to PHP conversion

    I needed help in converting following perl function into php:

    sub texttool {
    $tool =~ tr/A-Z/a-z/;
    @tool = split(/ /, $tool);
    foreach $word(@tool) {
    $tit1 = $word;
    $tit2 = $word;
    $tit1=(($tit1=~/(.{0,1})/) ? $1 : '');
    $tit2 = reverse $tit2;
    chop($tit2);
    $tit2 = reverse $tit2;
    $tit1 =~ tr/a-z/A-Z/;
    $word = "$tit1$tit2 ";
    }
    $tool = join(' ',@tool);
    }
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    Originally posted by zishan
    I needed help in converting following perl function into php:

    sub texttool {
    $tool =~ tr/A-Z/a-z/;
    @tool = split(/ /, $tool);
    foreach $word(@tool) {
    $tit1 = $word;
    $tit2 = $word;
    $tit1=(($tit1=~/(.{0,1})/) ? $1 : '');
    $tit2 = reverse $tit2;
    chop($tit2);
    $tit2 = reverse $tit2;
    $tit1 =~ tr/a-z/A-Z/;
    $word = "$tit1$tit2 ";
    }
    $tool = join(' ',@tool);
    }
    as far as i can tell in php it would be something like this
    [PHP]
    function texttool($tool) {
    $tool = strtolower($too l);
    $toolarr = split(' ',$tool);
    $toolarr2 = array()
    foreach($toolar r as $word){
    $tit1 = $word;
    $tit2 = $word;
    $tit2 = substr(strrev($ tit2),0,strlen( $tit2)-1);
    $tit1 = substr(strtoupp er($tit1),0,1);
    $toolarr2[] = $tit1.$tit2
    }
    return implode(' ',$toolarr2);
    }
    [/PHP]

    To write it out the textool accepts a string that gives it back capitalized with the first letter of the word and the rest of the word reversed with the last letter cut off.

    Comment

    Working...