Small function conversion from VB .NET to PHP

  • 14 years ago
    I am trying to covnert this function to PHP.
        Function Between(ByVal LookIn As String, ByVal A As String, ByVal B As String) As String
    Dim First As Integer
    Dim Last As Integer
    First = LookIn.IndexOf(A)
    Last = LookIn.LastIndexOf(B)

    'Check for potential values that could result in an error
    If First < 0 OrElse Last < 0 OrElse First > Last Then
    'A value would result in an error so return an empty string
    Return ""
    Else
    Return LookIn.Substring(First + A.Length, Last - (First + A.Length))
    End If
    End Function


    Here is the code I have, but it doesn't work right.

    function between($LookIn, $A, $B)
    {
    $First = substr($lookin, strstr($lookin, $a));
    $First = substr($lookin, strstr($lookin, $b));
    If ((First < 0) || (Last < 0) || (First > Last))
    {
    $ret_val = '';
    }
    Else
    {
    $a0 = strstr($LookIn, $First) + len($a);
    $a1 = $Last - ($First + len($a));
    $ret_val = substr($lookin,$a0, $a1);

    }
    }





































  • 14 years ago
    I have a few notes to make before you can see how I've edited the code. I've left comments there so that you'll always be reminded :)

    This is php. In php $a and $A are different symbols. php variable names are cAsE sEnSiTiVe.

    <?
       function between($LookIn, $A, $B)//same method header
       {
                   //note: strstr() returns a string from $A to the end of the string
                   //note: this is not vb, variables are cAsE sEnSiTiVe
                   //from the manual:
                   //string substr ( string $string, int $start [, int $length] )
                    //substr() returns the portion of string specified by the start and length parameters.
                    $indexFirst=strpos($LookIn, $A);//strpos returns the place in $lookin where $A occurs
                    $indexLast=strpos($LookIn, $B);//strpos returns the place in $lookin where $A occurs

            if (($indexFirst < 0) || ($indexLast < 0) || ($indexFirst > $indexLast))
            {
                $ret_val = '';
                    }
            else
            {
                $ret_val = substr($LookIn, $indexFirst, (strlen($LookIn)-$indexLast);
            }
            return $ret_val;//$ret_val should be between the start of $A and $B in string $LookIn
       }
    ?>


























  • 14 years ago
    Hi PyroInACage,

        I don't know really what you need! but, I wait to have helped! =]

        First, PHP is sensitive case! ohhhhhhh!
        len -> strlen();
        'First' is difference of $First.
    $First = substr($lookin, strstr($lookin, $a));
    $First = substr($lookin, strstr($lookin, $b)); //error of writing
    $a0 = strstr($LookIn, $First) + len($a);
    //this is wrong! $a0 -> need a type integer, OK!
    substr($lookin,$a0, $a1);


       
    <?
    function Between($LookIn, $a, $b)
       {
    if((!empty($a)) && (!empty($b)) && (!empty($LookIn))){
    $First = strpos($LookIn, $a);
    $Last = strpos($LookIn, $b);

    if (($First < 0) || ($Last < 0) || ($First > $Last))
    {
    $ret_val = 'empty';
    }
    else
    {
    $a0 = $First + strlen($a);
    $a1 = $Last - ($First + strlen($a));
    $ret_val = substr($LookIn, $a0, $a1);
    }
    }else{
    $ret_val = "The String A, B or LookIn is empty";
    }
    return $ret_val;
    }

    $funct = between("Hello Word!!!", "e", "W");
    echo $funct;
    ?>

    Kind Regards,
    Marcio Lins
    Solution Developer







































Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski