Perl to PHP

Discussion in 'PHP' started by star2323, Mar 2, 2006.

  1. #1
    Hello,

    I have some perl CGI scripts I'm changing over to PHP. I came across these statements in perl and I don't know exactly what they mean and how to convert them to PHP.

    Can anybody help?

    
    $pname = lc($array[1]);
    $pname = ucfirst($pname);
    $pname1 = $pname;
    $pname1 =~ s/ /-/g;
    
    Code (markup):
    What is the lc() function on the first line?
    What is the ucfirst() function on the second line?
    Then on line 4 what is ~ s/ /-/g; ?

    Thanks
     
    star2323, Mar 2, 2006 IP
  2. star2323

    star2323 Peon

    Messages:
    445
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok I figured out lc() in Perl is strtolower() in PHP and ucfirst() in Perl is the same in PHP.

    Lower Case = lc()
    Upper Case First character = ucfirst()

    But line 4 still has me puzzled.
     
    star2323, Mar 2, 2006 IP
  3. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #3
    lc converts a string to lower case, the PHP equivalent is the strtolower() function.

    ucfirst converts the first letter in each word into upper case. PHP equal is the ucwords function.

    The third line is a regular expression.... I was never good at those with Perl.
     
    tflight, Mar 2, 2006 IP
  4. star2323

    star2323 Peon

    Messages:
    445
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah I figured line 4 was a regex. I'm not real good at those either.
     
    star2323, Mar 2, 2006 IP
  5. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually, I think I'm wrong. s/ is a substitute thing. I think it is substituting each space (stuff between the first two slashes) with a dash (stuff between the second two slashes).
     
    tflight, Mar 2, 2006 IP
  6. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, it is searching for spaces, converting them to dashes, and the "g" specifies all possible matches in the string... not just the first one it finds.

    So I guess the PHP would be:

    $pname1 = str_replace(' ', '-', $pname1);
    Code (markup):
     
    tflight, Mar 2, 2006 IP
  7. jordie

    jordie Peon

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think $pname1 =~ s/ /-/g; is getting the first name, so it is splitting it by the first space or first dash. for php, try:

    $tmp = preg_split("/[\s\-]/", $pname1);
    $pname1 = $tmp[0];
     
    jordie, Mar 2, 2006 IP