...

View Full Version : Whats the difference between these functions?



Dubz
07-20-2012, 06:24 AM
A guy I know made a script that had these functions in it. They appear to do the same job, but my question is, whats the difference (besides that it uses different sub-functions)?


function strbet($inputstr, $deliLeft, $deliRight)
{
$posLeft = strpos($inputstr, $deliLeft) + strlen($deliLeft);
$posRight = strpos($inputstr, $deliRight, $posLeft);
return substr($inputstr, $posLeft, $posRight - $posLeft);
}
function stribet($inputstr, $deliLeft, $deliRight)
{
$posLeft = stripos($inputstr, $deliLeft) + strlen($deliLeft);
$posRight = stripos($inputstr, $deliRight, $posLeft);
return substr($inputstr, $posLeft, $posRight - $posLeft);
}



I tested it out with this code and it came out with the same results:


<?php
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$data = strbet($alphabet, 'h', 'q');
$data2 = stribet($alphabet, 'h', 'q');
echo $data."<br />\n".$data2;

function strbet($inputstr, $deliLeft, $deliRight)
{
$posLeft = strpos($inputstr, $deliLeft) + strlen($deliLeft);
$posRight = strpos($inputstr, $deliRight, $posLeft);
return substr($inputstr, $posLeft, $posRight - $posLeft);
}
function stribet($inputstr, $deliLeft, $deliRight)
{
$posLeft = stripos($inputstr, $deliLeft) + strlen($deliLeft);
$posRight = stripos($inputstr, $deliRight, $posLeft);
return substr($inputstr, $posLeft, $posRight - $posLeft);
}
?>

I guess the guy just made the function two ways, I don't know why but its not a big deal and I'll probably ask him why later.

RodionGork
07-20-2012, 07:40 AM
I tested it out with this code and it came out with the same results:

Funny way of exploring program or programming language. Have you got an idea of searching for manual with google "php stripos"?

http://php.net/manual/en/function.stripos.php

The second phrase gives an answer, I believe.

Dubz
07-20-2012, 09:10 PM
Funny way of exploring program or programming language. Have you got an idea of searching for manual with google "php stripos"?

http://php.net/manual/en/function.stripos.php

The second phrase gives an answer, I believe.

Well I was going to gogle them but i thought I knew what they did. When I asked my friend he told me that strbet is case-sensitive and stribet is not, so it all makes sense now :p

Fou-Lu
07-20-2012, 10:19 PM
Yep.
Although since the code is identical with the exception of the 'i' within it, it would probably be easier to write it into a single function that takes a boolean for case sensitivity, and use callbacks or reflection to determine what the result is. Something more simple:


function strbet($inputstr, $deliLeft, $deliRight, $bCaseSensitive = false)
{
$m = !$bCaseSensitive ? 'stripos' : 'strpos';
$posLeft = $m($inputstr, $deliLeft) + strlen($deliLeft);
$posRight = $m($inputstr, $deliRight, $posLeft);
// you really should verify if those returned valid results and not false
return substr($inputstr, $posLeft, $posRight - $posLeft);
}

Or you can even make that whatever name you want and chain the originals to it.

Take note of that comment I put in there. str[i]pos returns false on failure. False is equivalent to 0, so you cannot tell without using an if (false !== $posLeft) if the item is actually in the string at position 0 or if its not found at all.

Dubz
07-20-2012, 11:23 PM
// you really should verify if those returned valid results and not false


Before I call it I usually run a strpos() of the string to see what the content is going to look like, and I've only used this on webpages so the content isnt usually changed and i use html tags in the reference to do it. Either way, it works for me as long as i check it first.

Fou-Lu
07-21-2012, 12:05 AM
Yes, so long as there is a check. I'd do it on the strpos result myself, as that just kills two birds with one stone:


$str = 'cat';
$sFind = 'd';
if (false !== ($pos = strpos($str, $sFind)))
{
printf('%s was found in position %d of string %s', $sFind, $pos, $str);
}
else
{
printf('%s was not found in %s', $sFind, $str);
}


You can check for multiple strpos with multiple conditions in the if of course. So then you would only execute the substr if both are valid.

Dubz
07-21-2012, 06:42 AM
All I do is this


if(strpos($string, 'hello world') > -1)
echo '\'hello world\' exists!';
else
echo '\'hello world\' does not exist!';

For a quick example at least

Fou-Lu
07-21-2012, 04:12 PM
Since you are comparing to a negative value which the results of strpos can never be, and that negative values are always considered true, that will also work to determine if its not in the string.
I still don't like the loose comparisons though; I look at it the failure as a boolean and not as an integer. In the case that this data ends up in an int datatype, then the loose comparison will fail since 0 is greater than -1.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum