By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,636 Members | 2,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,636 IT Pros & Developers. It's quick & easy.

testing string for substring

P: n/a
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
......
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Bart Nessux wrote:
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
......


I got this to work:

$id_system = php_uname();
if (ereg("gentoo", "$id_system", $matches))
{
connect to this db
}
......
Jul 17 '05 #2

P: n/a
Bart Nessux <ba*********@hotmail.com> wrote:
Bart Nessux wrote:
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.


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
Jul 17 '05 #3

P: n/a
Bart Nessux wrote:
Bart Nessux wrote:
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
......


I got this to work:

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


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_system, "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.
Jul 17 '05 #4

P: n/a
Agelmar wrote:
Bart Nessux wrote:
Bart Nessux wrote:

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
......


I got this to work:

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

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_system, "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.


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

Jul 17 '05 #5

P: n/a
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" <ba*********@hotmail.com> wrote in message
news:c4**********@solaris.cc.vt.edu...
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
......

Jul 17 '05 #6

P: n/a
Matthias Drescher wrote:

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;
}

?>

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
Jul 17 '05 #7

P: n/a
Agelmar wrote:
Bart Nessux wrote:
Bart Nessux wrote:

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
......


I got this to work:

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

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_system, "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.


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

$id_system = php_uname();
$gentoo = "gentoo";
if (strpos($id_system, $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

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.