SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question MySQL FIND_IN_SET fuction port to PHP function

    I need MySQL FIND_IN_SET fuction port to PHP function. Thank you very much.

    http://dev.mysql.com/doc/refman/5.0/...on_find-in-set

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    stristr
    Have a look at some of the other string function as well
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you need to find the position of a certain string within a comma-delimited list of strings (exactly as FIND_IN_SET operates), then this should work:
    PHP Code:
    <?php
        $needle 
    "b";
        
    $haystack "a,b,c,d";

        
    $values explode(","$haystack);

        
    $position 0;
        if (
    in_array($needle$values))
        {
            
    $flipped array_flip($values);
            
    $position $flipped[$needle] + 1;
        }

        echo 
    "$needle is in position $position";
    ?>
    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    array_search($strexplode(','$strlist)); 
    array_search

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much.

    My solution:
    PHP Code:
    if( strpos(','.$haystack.',',','.$needle.',') ){
        return 
    true;


  6. #6
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just want to find out whether the value exists in the string:
    PHP Code:
    $needle "b";
    $haystack "a,b,c,d";

    return 
    in_array($needleexplode(","$haystack)); 
    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lkn2 View Post
    Thank you very much.

    My solution:
    PHP Code:
    if( strpos(','.$haystack.',',','.$needle.',') ){
        return 
    true;

    Should be "if(false !== strpos(...))", see strpos manual page.


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
  •