testing string for substring

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

    #1

    testing string for substring

    I have some php scripts that I use on several machines. Each machine has an
    identical copy of a mysql database that the scripts run against. The only
    differences in the DBs are the user names and passwords (they all differ).
    Fortunately, each machine is a different version of Unix or Linux, so I've
    instructed the script to do a uname test before connecting to the local
    database. However, I don't know how to test the variable that contains the
    uname string.

    $x = php_uname();
    print $x;
    // The print looks like this:
    Linux localhost 2.4.22-gentoo-r5 #4 Mon Feb 2 18:28:44 EST 2004 i686
    if ($x contains "gentoo") // How do I test this string for "gentoo"
    {
    connect to this db
    }
    else
    ......
  • Bart Nessux

    #2
    Re: testing string for substring

    Bart Nessux wrote:
    [color=blue]
    > I have some php scripts that I use on several machines. Each machine has
    > an identical copy of a mysql database that the scripts run against. The
    > only differences in the DBs are the user names and passwords (they all
    > differ). Fortunately, each machine is a different version of Unix or
    > Linux, so I've instructed the script to do a uname test before connecting
    > to the local database. However, I don't know how to test the variable that
    > contains the uname string.
    >
    > $x = php_uname();
    > print $x;
    > // The print looks like this:
    > Linux localhost 2.4.22-gentoo-r5 #4 Mon Feb 2 18:28:44 EST 2004 i686
    > if ($x contains "gentoo") // How do I test this string for "gentoo"
    > {
    > connect to this db
    > }
    > else
    > ......[/color]

    I got this to work:

    $id_system = php_uname();
    if (ereg("gentoo", "$id_system ", $matches))
    {
    connect to this db
    }
    ......

    Comment

    • Billy Harvey

      #3
      Re: testing string for substring

      Bart Nessux <bart_nessux@ho tmail.com> wrote:[color=blue]
      >Bart Nessux wrote:
      >[color=green]
      >> I have some php scripts that I use on several machines. Each machine has
      >> an identical copy of a mysql database that the scripts run against. The
      >> only differences in the DBs are the user names and passwords (they all
      >> differ). Fortunately, each machine is a different version of Unix or
      >> Linux, so I've instructed the script to do a uname test before connecting
      >> to the local database. However, I don't know how to test the variable that
      >> contains the uname string.[/color][/color]

      You might have more success with:

      <?
      $x = posix_uname();
      switch($x) {
      case "machine1":
      ...
      ?>

      The nodename should be guaranteed to be unique in a given network.

      Billy

      Comment

      • Agelmar

        #4
        Re: testing string for substring

        Bart Nessux wrote:[color=blue]
        > Bart Nessux wrote:
        >[color=green]
        >> I have some php scripts that I use on several machines. Each machine
        >> has an identical copy of a mysql database that the scripts run
        >> against. The only differences in the DBs are the user names and
        >> passwords (they all differ). Fortunately, each machine is a
        >> different version of Unix or Linux, so I've instructed the script to
        >> do a uname test before connecting to the local database. However, I
        >> don't know how to test the variable that contains the uname string.
        >>
        >> $x = php_uname();
        >> print $x;
        >> // The print looks like this:
        >> Linux localhost 2.4.22-gentoo-r5 #4 Mon Feb 2 18:28:44 EST 2004
        >> i686 if ($x contains "gentoo") // How do I test this string
        >> for "gentoo" {
        >> connect to this db
        >> }
        >> else
        >> ......[/color]
        >
        > I got this to work:
        >
        > $id_system = php_uname();
        > if (ereg("gentoo", "$id_system ", $matches))
        > {
        > connect to this db
        > }
        > ......[/color]

        Do not use ereg. Ereg is doing regular expression matches, which is much
        more powerful (and slower) than what you're looking for. Try simply doing
        if(strstr($id_s ystem, "gentoo"))

        And btw, putting a variable in quotes is another big performance hit -
        there's no need, essentially you're forcing PHP to make a string that has
        the variable in it, when the variable itself is already a string in this
        case.


        Comment

        • Bart Nessux

          #5
          Re: testing string for substring

          Agelmar wrote:[color=blue]
          > Bart Nessux wrote:
          >[color=green]
          >>Bart Nessux wrote:
          >>
          >>[color=darkred]
          >>>I have some php scripts that I use on several machines. Each machine
          >>>has an identical copy of a mysql database that the scripts run
          >>>against. The only differences in the DBs are the user names and
          >>>passwords (they all differ). Fortunately, each machine is a
          >>>different version of Unix or Linux, so I've instructed the script to
          >>>do a uname test before connecting to the local database. However, I
          >>>don't know how to test the variable that contains the uname string.
          >>>
          >>> $x = php_uname();
          >>> print $x;
          >>> // The print looks like this:
          >>> Linux localhost 2.4.22-gentoo-r5 #4 Mon Feb 2 18:28:44 EST 2004
          >>> i686 if ($x contains "gentoo") // How do I test this string
          >>> for "gentoo" {
          >>> connect to this db
          >>> }
          >>> else
          >>> ......[/color]
          >>
          >>I got this to work:
          >>
          >> $id_system = php_uname();
          >> if (ereg("gentoo", "$id_system ", $matches))
          >> {
          >> connect to this db
          >> }
          >> ......[/color]
          >
          >
          > Do not use ereg. Ereg is doing regular expression matches, which is much
          > more powerful (and slower) than what you're looking for. Try simply doing
          > if(strstr($id_s ystem, "gentoo"))
          >
          > And btw, putting a variable in quotes is another big performance hit -
          > there's no need, essentially you're forcing PHP to make a string that has
          > the variable in it, when the variable itself is already a string in this
          > case.
          >
          >[/color]

          Thank you for the advice. Performance isn't an issue for me yet, but it
          will be someday.

          Comment

          • Matthias Drescher

            #6
            Re: testing string for substring

            hi bart,

            why don´´t you try this with the split function ?
            array split ( string pattern, string string [, int limit])

            In your case:

            <?php
            list($v1, $v2, $v3) = split("-", $x, 3);

            switch($x)
            {
            case "gentoo":
            do something....
            break;
            default:
            break;
            }

            ?>

            regards Matthias


            "Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
            news:c4adpn$20p $1@solaris.cc.v t.edu...[color=blue]
            > I have some php scripts that I use on several machines. Each machine has[/color]
            an[color=blue]
            > identical copy of a mysql database that the scripts run against. The only
            > differences in the DBs are the user names and passwords (they all differ).
            > Fortunately, each machine is a different version of Unix or Linux, so I've
            > instructed the script to do a uname test before connecting to the local
            > database. However, I don't know how to test the variable that contains the
            > uname string.
            >
            > $x = php_uname();
            > print $x;
            > // The print looks like this:
            > Linux localhost 2.4.22-gentoo-r5 #4 Mon Feb 2 18:28:44 EST 2004 i686
            > if ($x contains "gentoo") // How do I test this string for "gentoo"
            > {
            > connect to this db
            > }
            > else
            > ......[/color]


            Comment

            • Default User

              #7
              Re: testing string for substring

              Matthias Drescher wrote:[color=blue]
              >
              > hi bart,
              >
              > why don´´t you try this with the split function ?
              > array split ( string pattern, string string [, int limit])
              >
              > In your case:
              >
              > <?php
              > list($v1, $v2, $v3) = split("-", $x, 3);
              >
              > switch($x)
              > {
              > case "gentoo":
              > do something....
              > break;
              > default:
              > break;
              > }
              >
              > ?>[/color]


              Unless you need the other parts of the name, this seems like a
              needlessly expensive way to go. A simple strstr() on the name should be
              much more efficient.



              Brian Rodenborn

              Comment

              • Bart Nessux

                #8
                Re: testing string for substring

                Agelmar wrote:[color=blue]
                > Bart Nessux wrote:
                >[color=green]
                >>Bart Nessux wrote:
                >>
                >>[color=darkred]
                >>>I have some php scripts that I use on several machines. Each machine
                >>>has an identical copy of a mysql database that the scripts run
                >>>against. The only differences in the DBs are the user names and
                >>>passwords (they all differ). Fortunately, each machine is a
                >>>different version of Unix or Linux, so I've instructed the script to
                >>>do a uname test before connecting to the local database. However, I
                >>>don't know how to test the variable that contains the uname string.
                >>>
                >>> $x = php_uname();
                >>> print $x;
                >>> // The print looks like this:
                >>> Linux localhost 2.4.22-gentoo-r5 #4 Mon Feb 2 18:28:44 EST 2004
                >>> i686 if ($x contains "gentoo") // How do I test this string
                >>> for "gentoo" {
                >>> connect to this db
                >>> }
                >>> else
                >>> ......[/color]
                >>
                >>I got this to work:
                >>
                >> $id_system = php_uname();
                >> if (ereg("gentoo", "$id_system ", $matches))
                >> {
                >> connect to this db
                >> }
                >> ......[/color]
                >
                >
                > Do not use ereg. Ereg is doing regular expression matches, which is much
                > more powerful (and slower) than what you're looking for. Try simply doing
                > if(strstr($id_s ystem, "gentoo"))
                >
                > And btw, putting a variable in quotes is another big performance hit -
                > there's no need, essentially you're forcing PHP to make a string that has
                > the variable in it, when the variable itself is already a string in this
                > case.
                >
                >[/color]

                After reading about strstr, I decided to use strpos like this:

                $id_system = php_uname();
                $gentoo = "gentoo";
                if (strpos($id_sys tem, $gentoo))
                {
                connect to this db;
                }
                else
                }
                connect to this db;

                The PHP online documentation is (by far) the best I've ever seen. I
                program a lot in Python which is much more intuitive than PHP, but it's
                documentation does not compare to PHP's. Even though PHP has a more
                complex and difficult structure and syntax than Python, its
                documentation makes learning and doing *very* easy.

                Thanks,

                Bart

                Comment

                Working...