PHP String Thing

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

    PHP String Thing

    Say I've got this:

    <?php

    $string = "Bill,Martin,Jo e";
    $name1
    $name2
    $name3

    ?>

    How do I make the $name1 equal to the first name in the list of
    strings, $name2 the second and so on?

    Thank You,
    Trevor

  • Duyet The Vo

    #2
    Re: PHP String Thing

    $string = "Bill,Martin,Jo e";

    list ($name1,$name2, $name3) = split ( ",", $string );

    "Trevor" <trevordixon@gm ail.com> wrote in message
    news:1108280762 .365123.210860@ l41g2000cwc.goo glegroups.com.. .[color=blue]
    > Say I've got this:
    >
    > <?php
    >
    > $string = "Bill,Martin,Jo e";
    > $name1
    > $name2
    > $name3
    >
    > ?>
    >
    > How do I make the $name1 equal to the first name in the list of
    > strings, $name2 the second and so on?
    >
    > Thank You,
    > Trevor
    >[/color]


    Comment

    • Dave Patton

      #3
      Re: PHP String Thing

      "Trevor" <trevordixon@gm ail.com> wrote in
      news:1108280762 .365123.210860@ l41g2000cwc.goo glegroups.com:
      [color=blue]
      > Say I've got this:
      >
      ><?php
      >
      > $string = "Bill,Martin,Jo e";
      > $name1
      > $name2
      > $name3
      >
      > ?>[/color]

      OK, we'll say that you have a PHP script that has one line
      of code followed by three lines with parse errros ;-)
      [color=blue]
      > How do I make the $name1 equal to the first name in the list of
      > strings, $name2 the second and so on?[/color]




      --
      Dave Patton
      Canadian Coordinator, Degree Confluence Project
      The Degree Confluence Project contains photographs of the intersections of integer latitude and longitude degree lines.

      My website: http://members.shaw.ca/davepatton/

      Comment

      • dourdoun

        #4
        Re: PHP String Thing

        Simple Answer:

        <?php
        $string = "Bill,Martin,Jo e";
        list($name1, $name2, $name3) = explode(",", $string);
        ?>

        Complicated, for any number of varibles:

        <?php
        $string = "Bill,Martin,Jo e";
        $names = explode(",", $string);

        for($i = 0; $i < count($names); $i++)
        {
        eval('$name'.($ i+1).' = "'.$names[$i].'";');
        }

        print $name1; // Prints 'Bill'
        print $name2; // Prints 'Martin'
        print $name3; // Prints 'Joe'
        ?>

        hope it helps,

        Vasilis



        "Trevor" <trevordixon@gm ail.com> wrote in message news:<110828076 2.365123.210860 @l41g2000cwc.go oglegroups.com> ...[color=blue]
        > Say I've got this:
        >
        > <?php
        >
        > $string = "Bill,Martin,Jo e";
        > $name1
        > $name2
        > $name3
        >
        > ?>
        >
        > How do I make the $name1 equal to the first name in the list of
        > strings, $name2 the second and so on?
        >
        > Thank You,
        > Trevor[/color]

        Comment

        • Janwillem Borleffs

          #5
          Re: PHP String Thing

          dourdoun wrote:[color=blue]
          > for($i = 0; $i < count($names); $i++)
          > {
          > eval('$name'.($ i+1).' = "'.$names[$i].'";');
          > }
          >[/color]

          You don't need eval for this:

          for ($i = 0; $i < count($names); $i++) {
          ${'name'.($i+1) } = $names[$i];
          }


          JW



          Comment

          • Trevor

            #6
            Re: PHP String Thing

            Thank you very much, that's what I needed.

            Trevor

            Comment

            Working...