473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
......
Jul 17 '05 #1
7 3529
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Garfield | last post by:
Hello I have a function that returns a string array. The string has a name and an ID number, they are separated by a comma. I cannot for the life of me by using the string methods get to...
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
4
by: Michael McCarthy | last post by:
I want to test for the ~possible~ occurance of a string within another string... IndexOfAny gives wildly odd results, possibly because it's expecting the string to be there... This is probably...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
9
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.