Adding single quotes around items in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Sharman

    Adding single quotes around items in a list

    Is there a PHP function which can add single quotes around each item
    in a list?

    So if I have:

    $myList = "michael,frank, peter,sally,jun e";

    What I want to end up with is:

    $myList = "'michael','fra nk','peter','sa lly','june'";

    I know I can loop over the list and do it manually, I was wondering if
    there was a cool function to do it automatically.

    I want to add this to the "IN" part of a database "WHERE" clause.

    Thanks in advance

  • ELINTPimp

    #2
    Re: Adding single quotes around items in a list

    On May 26, 6:48 am, Michael Sharman <sha...@gmail.c omwrote:
    Is there a PHP function which can add single quotes around each item
    in a list?
    >
    So if I have:
    >
    $myList = "michael,frank, peter,sally,jun e";
    >
    What I want to end up with is:
    >
    $myList = "'michael','fra nk','peter','sa lly','june'";
    >
    I know I can loop over the list and do it manually, I was wondering if
    there was a cool function to do it automatically.
    >
    I want to add this to the "IN" part of a database "WHERE" clause.
    >
    Thanks in advance
    Use array_walk and implement the change in a callback function.
    Apply a user supplied function to every member of an array

    <?php

    //modified $myList to make it an actual array
    $myList = array("michael" ,"frank","peter ","sally","june ");

    function addWrapper (&$value, $key, $wrapper) {
    $value = $wrapper.$value .$wrapper;
    //no return, passed by reference
    }

    array_walk($myL ist, 'addWrapper', "'");
    print_r($myList );
    ?>

    Comment

    • sheldonlg

      #3
      Re: Adding single quotes around items in a list

      Michael Sharman wrote:
      Is there a PHP function which can add single quotes around each item
      in a list?
      >
      So if I have:
      >
      $myList = "michael,frank, peter,sally,jun e";
      >
      What I want to end up with is:
      >
      $myList = "'michael','fra nk','peter','sa lly','june'";
      >
      I know I can loop over the list and do it manually, I was wondering if
      there was a cool function to do it automatically.
      >
      I want to add this to the "IN" part of a database "WHERE" clause.
      >
      Thanks in advance
      >

      I haven't tested it, but you might try this:

      IN "('" . str_replace("," , "','", $myList) . "')"

      for the IN part of the WHERE clause.

      Comment

      • Michael Fesser

        #4
        Re: Adding single quotes around items in a list

        ..oO(Michael Sharman)
        >Is there a PHP function which can add single quotes around each item
        >in a list?
        >
        >So if I have:
        >
        >$myList = "michael,frank, peter,sally,jun e";
        >
        >What I want to end up with is:
        >
        >$myList = "'michael','fra nk','peter','sa lly','june'";
        >
        >I know I can loop over the list and do it manually, I was wondering if
        >there was a cool function to do it automatically.
        You could replace the commas with ',' or use a combination of explode()
        and implode():

        $myList = "'".str_replace (",", "','", $myList)."'";

        or

        $myList = "'".implode("', '", explode(",", $myList))."'";

        HTH
        Micha

        Comment

        • Michael Sharman

          #5
          Re: Adding single quotes around items in a list

          On May 26, 9:14 pm, Michael Fesser <neti...@gmx.de wrote:
          .oO(Michael Sharman)
          >
          Is there a PHP function which can add single quotes around each item
          in a list?
          >
          So if I have:
          >
          $myList = "michael,frank, peter,sally,jun e";
          >
          What I want to end up with is:
          >
          $myList = "'michael','fra nk','peter','sa lly','june'";
          >
          I know I can loop over the list and do it manually, I was wondering if
          there was a cool function to do it automatically.
          >
          You could replace the commas with ',' or use a combination of explode()
          and implode():
          >
          $myList = "'".str_replace (",", "','", $myList)."'";
          >
          or
          >
          $myList = "'".implode("', '", explode(",", $myList))."'";
          >
          HTH
          Micha
          Thanks all for the great ideas, I ended up going with:

          IN('" . str_replace("," , "','", $myList) . "')"

          That was the simplest for what I was doing.

          Comment

          • sheldonlg

            #6
            Re: Adding single quotes around items in a list

            Michael Sharman wrote:
            On May 26, 9:14 pm, Michael Fesser <neti...@gmx.de wrote:
            >.oO(Michael Sharman)
            >>
            >>Is there a PHP function which can add single quotes around each item
            >>in a list?
            >>So if I have:
            >>$myList = "michael,frank, peter,sally,jun e";
            >>What I want to end up with is:
            >>$myList = "'michael','fra nk','peter','sa lly','june'";
            >>I know I can loop over the list and do it manually, I was wondering if
            >>there was a cool function to do it automatically.
            >You could replace the commas with ',' or use a combination of explode()
            >and implode():
            >>
            >$myList = "'".str_replace (",", "','", $myList)."'";
            >>
            >or
            >>
            >$myList = "'".implode("', '", explode(",", $myList))."'";
            >>
            >HTH
            >Micha
            >
            Thanks all for the great ideas, I ended up going with:
            >
            IN('" . str_replace("," , "','", $myList) . "')"
            >
            That was the simplest for what I was doing.

            I assume there was a " before the IN or a " followed by the previous
            part of the query before the IN.

            Comment

            • Michael Sharman

              #7
              Re: Adding single quotes around items in a list

              On May 27, 4:50 am, sheldonlg <sheldonlgwrote :
              Michael Sharman wrote:
              On May 26, 9:14 pm, Michael Fesser <neti...@gmx.de wrote:
              .oO(Michael Sharman)
              >
              >Is there a PHP function which can add single quotes around each item
              >in a list?
              >So if I have:
              >$myList = "michael,frank, peter,sally,jun e";
              >What I want to end up with is:
              >$myList = "'michael','fra nk','peter','sa lly','june'";
              >I know I can loop over the list and do it manually, I was wondering if
              >there was a cool function to do it automatically.
              You could replace the commas with ',' or use a combination of explode()
              and implode():
              >
              $myList = "'".str_replace (",", "','", $myList)."'";
              >
              or
              >
              $myList = "'".implode("', '", explode(",", $myList))."'";
              >
              HTH
              Micha
              >
              Thanks all for the great ideas, I ended up going with:
              >
              IN('" . str_replace("," , "','", $myList) . "')"
              >
              That was the simplest for what I was doing.
              >
              I assume there was a " before the IN or a " followed by the previous
              part of the query before the IN.
              There was, sorry...I should have posted the complete source.

              Thanks sheldonlg

              Comment

              • Joseph Taylor

                #8
                Re: Adding single quotes around items in a list

                On May 26, 3:48 am, Michael Sharman <sha...@gmail.c omwrote:
                Is there a PHP function which can add single quotes around each item
                in a list?
                >
                So if I have:
                >
                $myList = "michael,frank, peter,sally,jun e";
                >
                What I want to end up with is:
                >
                $myList = "'michael','fra nk','peter','sa lly','june'";
                >
                I know I can loop over the list and do it manually, I was wondering if
                there was a cool function to do it automatically.
                >
                I want to add this to the "IN" part of a database "WHERE" clause.
                >
                Thanks in advance
                This is a bit late, but...

                If $my_list is tainted (i.e. it originated from the user) you'd be
                wise to escape the list items. I've attached two ways of doing this.
                The first uses implode/explode and array_map (I call this the
                "functional " approach) and the second uses preg_replace with a
                callback.

                <?php

                $my_list = "michael,frank, peter,sally,jun e";

                // =============== =============== =======
                // = First Method, Somewhat Functional =
                // =============== =============== =======

                $my_new_list = implode(
                ',',
                array_map(
                create_function (
                '$a',
                'return "\'"
                . mysql_real_esca pe_string($a)
                . "\'";'
                ),
                explode(',', $my_list)
                )
                );

                // =============== ==============
                // = Second Method, With Regex =
                // =============== ==============

                $my_newer_list = preg_replace_ca llback(
                '/(^|,)([^,]+)/',
                create_function (
                '$matches',
                'return "$matches[1]\'"
                . mysql_real_esca pe_string($matc hes[2])
                . "\'";'
                ),
                $my_list
                );

                // ==========
                // = Result =
                // ==========

                echo $my_new_list, "\n", $my_newer_list;

                ?>

                jt

                Comment

                Working...