For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > March 2004 > testing string for substring









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author testing string for substring
Bart Nessux

2004-03-29, 8:32 pm

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

2004-03-29, 8:32 pm

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
}
......
Billy Harvey

2004-03-29, 9:31 pm

Bart Nessux <bart_nessux@hotmail.com> wrote:[color=darkred]
>Bart Nessux wrote:
>

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
Agelmar

2004-03-29, 10:30 pm

Bart Nessux wrote:
> Bart Nessux wrote:
>
>
> 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.


Bart Nessux

2004-03-30, 9:37 am

Agelmar wrote:
> Bart Nessux wrote:
>
>
>
> 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.

Matthias Drescher

2004-03-30, 9:37 am

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@hotmail.com> wrote in message
news:c4adpn$20p$1@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
> ......



Default User

2004-03-30, 4:31 pm

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
Bart Nessux

2004-03-31, 12:33 pm

Agelmar wrote:
> Bart Nessux wrote:
>
>
>
> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com