PDA

Click to See Complete Forum and Search --> : a function from VB to PHP[solved]


kill_bill_gates
Mar 5th, 2005, 05:27 AM
hi, can somebody convert the VB code below to php

Private Function Between(Begining As String, Ending As String, TextToLookIn As String) As String
Dim Be, En, TTLI As String
Be = Begining
En = Ending
TTLI = TextToLookIn
TTLI = Right(TTLI, Len(TTLI) - (InStr(1, TTLI, Be) + Len(Be) - 1))
Between = Left(TTLI, InStr(1, TTLI, En) - 1)
End Function

visualAd
Mar 5th, 2005, 05:30 AM
What does it do?

kill_bill_gates
Mar 5th, 2005, 05:32 AM
it returns the string value between "Beginning" and "Ending" variables, in the given text.

visualAd
Mar 5th, 2005, 05:40 AM
So if I have a string:

$str = 'This is a test string with some random words';

echo(Between('test', 'random', $str));

The ouput would be:

"string with some" ?

kill_bill_gates
Mar 5th, 2005, 05:55 AM
yes
can you do it in PHP?

visualAd
Mar 5th, 2005, 06:32 AM
Done:

function between($start, $end, $string)
{
$pos1 = strpos($string, $start) + strlen($start);
$pos2 = strpos($string, $end) - 1;

return substr($string, $pos1, $pos2 - $pos1 + 1);
}

I'll let you decide its behviour if one or both the search strings are not found.

kill_bill_gates
Mar 5th, 2005, 06:40 AM
wow thanks.
I'm kind of a newbie in PHP, can you explain the functions you used?

thegreatone
Mar 5th, 2005, 06:44 AM
Bill Gates killer, that is in PHP... just add the
<?
?>

tags to it...

visualAd
Mar 5th, 2005, 06:47 AM
strpos (http://www.php.net/strpos) = (VB) InStr - it returns the index (0 based) of the first occurence of the search string.

substr (http://www.php.net/substr) = (VB) Mid - which returns a portion of a stirng, given a start position and the length all be it more powerful than VB's Mid function. ;)

The strpos function returns false if it cannot find a string so to set that you would need to use if($pos1 === false).

kill_bill_gates
Mar 5th, 2005, 08:26 AM
thanks VisualAd

Bill Gates killer, that is in PHP... just add the
<?
?>

tags to it...
what did you mean by that :confused: ? I know that's PHP.