Professional Web Applications Themes

php equivalent to asp's instr() - PHP Development

Hmm... Are you sure strpos doesn't work for you? Sure worked for me when I wanted something like that Otherwise, checkout following: [url]http://se2.php.net/strings[/url] function instr($needle, $haystack,$case_sensitive=true) { return (($case_sensitive?strpos($needle,$haystack):stripo s($needle,$haystack))!==fa lse); } try that, although, that's just from the top of my head, not tested or anything and it's my pretty bad coding style... also, take a look at ereg()... -- // DvDmanDT MSN: [email]dvdmandthotmail.com[/email] Mail: [email]dvdmandttelia.com[/email] "Chris W. Parker" <cparkerswatgear.com> skrev i meddelandet news:001BD19C96E6E64E8750D72C2EA0ECEE1AE1D8ati-ex-01.ati.local... Hi. I don't know why but I've had the darndest time trying to find an easy equivalent of asp's InStr() function in PHP. I know ...

  1. #1

    Default Re: php equivalent to asp's instr()

    Hmm... Are you sure strpos doesn't work for you? Sure worked for me when I
    wanted something like that Otherwise, checkout following:
    [url]http://se2.php.net/strings[/url]

    function instr($needle, $haystack,$case_sensitive=true)
    {
    return
    (($case_sensitive?strpos($needle,$haystack):stripo s($needle,$haystack))!==fa
    lse);
    }
    try that, although, that's just from the top of my head, not tested or
    anything and it's my pretty bad coding style...

    also, take a look at ereg()...
    --
    // DvDmanDT
    MSN: [email]dvdmandthotmail.com[/email]
    Mail: [email]dvdmandttelia.com[/email]
    "Chris W. Parker" <cparkerswatgear.com> skrev i meddelandet
    news:001BD19C96E6E64E8750D72C2EA0ECEE1AE1D8ati-ex-01.ati.local...
    Hi.

    I don't know why but I've had the darndest time trying to find an easy
    equivalent of asp's InStr() function in PHP. I know there is
    preg_match(), strpos(), and strstr(), but none of them work like I want
    them to.

    In fact, except for maybe preg_match(), I can't get any of them to work
    right.

    I want a function that will return true if the string is found, and
    false if it is not found.

    Like this:*

    $needle = "&";
    $haystack = "Belle & Sebastian";

    if(FoundInString($needle, $haystack))
    {
    echo "found!";
    }
    else
    {
    echo "not found!";
    }


    Anyone have a function to share?


    Thanks,
    Chris.


    * Anyone catch the Belle & Sebastian show at the Greek Theatre in LA
    this past sunday?
    Dvdmandt Guest

  2. #2

    Default Re: php equivalent to asp's instr()

    Hi Chris,

    I've always used strstr() for this _exact_ same purpose and it's worked just
    fine. I find it much more useful as an InStr()-like function than for
    actually doing anything with the string it returns. You'll want to note
    that the parameter order is $haystack before $needle. That goes for
    strpos() as well. I noticed that in your original post, you're using a
    hypothetical function FoundInString($needle,$haystack). If you used the
    same order ($needle,$haystack) when you tried strstr() or strpos(), then it
    would fail because it's a bit difficult to find a haystack in a needle. =/

    I _strongly_ recommend using one of these built-in functions, as they will
    run much faster than any programmer-defined function. Below I will explain
    how and potential pitfalls.

    Only problem I noticed is that you want the function to return an explicit
    true if the string is found (and false if it is not). Both functions will
    return an explicit false if the needle is not found, but neither return an
    explicit true. Don't let that stop you from using them though. If needle
    is found, strstr() returns a string and strpos() returns an integer
    (possibly 0). An if statement will consider a string (as returned by
    strstr()) as logically true, so you can use strstr() by itself inside an if
    statement. strpos() however returns the offset from the beginning of
    $haystack where it found $needle. This means that if $needle is at the
    beginning of $haystack, then it will return an integer 0. This of course is
    considered false by an if statement. However, you can use
    if(strpos($haystack,$needle)!==false). If you take a look at
    [url]http://us3.php.net/manual/en/language.operators.comparison.php[/url], you'll see
    that PHP's $a===$b and $a!==$b operators result in a true only if $a and $b
    are of the same type (bool, int, etc). Since an integer 0 is not the same
    as a boolean false, strpos()!==false will not choke when it returns an
    integer 0.

    If you want the function to return an explicit true because you want to
    assign the result to a variable, you can do this:
    $foo = (strstr($haystack,$needle)!==false)
    or anywhere else you plan to use it for that matter.

    Doentation for strstr() and strpos()
    [url]http://us3.php.net/strstr[/url]
    [url]http://us3.php.net/strpos[/url]
    David Cuddeback Guest

  3. #3

    Default Re: php equivalent to asp's instr()

    function inStr($searchIn,$stringToSearch){
    $x=-1;
    $x=strpos(strtoupper($searchIn),strtoupper($string ToSearch));
    if ($x>-1) return TRUE; else return FALSE;
    }




    * Anyone catch the Belle & Sebastian show at the Greek Theatre in LA
    this past sunday?[/QUOTE]
    Unregistered Guest

Similar Threads

  1. advice on how to handle INSTR search?
    By bobmct in forum MySQL
    Replies: 0
    Last Post: June 9th, 05:23 PM
  2. sql greatest,decode,instr. minimize code lines sql
    By zeny xu in forum Coldfusion Database Access
    Replies: 3
    Last Post: April 25th, 07:00 PM
  3. INSTR function and case sensitivity
    By D Woods in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: March 14th, 06:36 AM
  4. InStr, substr, Mid ???
    By J. Muenchbourg in forum ASP
    Replies: 1
    Last Post: August 13th, 12:18 AM
  5. how to print instr ptr from stack in signal handler?
    By Chris Markle in forum UNIX Programming
    Replies: 0
    Last Post: August 1st, 03:50 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139