Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Question Whats the difference between these functions?

    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)?
    PHP Code:
    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);

    Edit:
    I tested it out with this code and it came out with the same results:
    PHP Code:
    <?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.
    Last edited by Dubz; 07-20-2012 at 06:37 AM.

  • #2
    New Coder
    Join Date
    Mar 2012
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by RodionGork View Post
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
    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.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
        // 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.
    Last edited by Dubz; 07-20-2012 at 11:32 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
    $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.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    All I do is this
    PHP Code:
    if(strpos($string'hello world') > -1)
        echo 
    '\'hello world\' exists!';
    else
        echo 
    '\'hello world\' does not exist!'
    For a quick example at least

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •