php-general Digest 25 Dec 2007 09:36:43 -0000 Issue 5199

Topics (messages 266258 through 266268):

Re: Opinion about the using $GLOBALS directly
        266258 by: Sancar Saran
        266259 by: Robert Cummings

Re: building PHP5.2.5 on Mac OS X Leopard (anyone know how to build a just an 
extension)
        266260 by: Jochem Maas

loadHTML()
        266261 by: M5
        266262 by: Casey
        266263 by: Casey
        266264 by: M5

Simple RegEx question
        266265 by: M5
        266266 by: Casey
        266267 by: Casey
        266268 by: Jochem Maas

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
Hello Tedd,

Here my opinoins

First of all, I ask this question to is there any technical dead end for using 
$GLOBALS directly.

It seems not.

And I believe other arguments was closely connected to coding style.

I'm self learner, I learn evrything about computers, programming (that means 
PHP) and English by myself. Because of this time to time I found my self into 
very alien position for others.

On Monday 24 December 2007 19:13:07 tedd wrote:
> At 4:18 PM +0200 12/19/07, Sancar Saran wrote:
> >  > that said, avoid globals like the plague - sometimes you may come up
> >  > with
> >>
> >>  a situation where using a global is really necessary - such situations
> >>  should be the exception rather than the rule, often if your thinking of
> >>  using a global there is another way of doing it. jmho
> >
> >And this is why I'm asking here, WHY I should avoid globals like plague ?
> >Every one says bad. Alright, but no one says WHY ?
>
> Hi:
>
> I'm a little late to this thread (been busy), but this is why I
> rarely use globals and have not used them in php.
>
> One of the fundamentals of programming I've learned is to reduce
> problem/solution to their most basic form. Simply, you see a problem
> and you solve it by dividing the problem it into smaller parts and
> then writing code to solve the small parts. Eventually, all the small
> solutions come together to solve the larger problem.

Absolutely. I learn this from very hard way. (rewriting the code :))

> Certainly, and it seems even logical, that you can use a global
> variable to communicate between the different parts and everything
> should work -- IF -- that's all there was to it. But, if you have to
> debug the code OR if you want to use a portion of that solution to
> solve a different problem, then you can have difficulties. For
> example:
>
> [1] If you have to debug the code, there's not a really good way to
> look at a function and see if a variable in it is a global or not --
> therein lies a problem, you don't readily know.
>
> In other languages I adopted a naming convention that used "g" as the
> first letter of a Global variable, like gThisIsMyGolbalVariable --
> that way at least I knew the variable was global. But, even then I
> didn't know where the global was defined or where it might be changed
> -- it was just an unknown in my function that I had to consider.

$GLOBALS['myVariable'] directly solves the problem. 

> [2] If you want to reused a portion of the code, then you have to
> also accommodate the global. This makes transporting your code from
> one application to another problematic because you never know IF your
> function replies on a global or not. If you never use globals, then
> that's never a problem.

If is there any possibility to use the function with some other variables I 
use it to.

> So, my advice is to not use globals, but rather make the functions
> independent from globals. That way you troubleshoot them easier and
> can cut/paste them into other solutions without having to worry if
> some part of that function relies on something who knows where.
>
> That's my reasons why I avoid globals.
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com

Here Some basic list for my $GLOBALS usage.

$GLOBALS['db'] =  my Adodb object
$GLOBALS['mc'] =  my Memcached object
$GLOBALS['l'] =  my Language keys
$GLOBALS['d'] = my debug pool. 
$GLOBALS['c'] = my config array.

And someting like that.

A very basic function for pooling debugs.

function addDebug($_sDebugVal) {
                $GLOBALS['d']['_DMSG'][] = $_sDebugVal;
}

usage addDebug($MyVar); or addDebug(print_r($arrayFoo,true));

end of page 

getDebug() {
        return $GLOBALS['l']['_debug_messages_']."<br/><pre>".implode("\n".
$GLOBALS['d']['_DMSG'])."</pre>";
}

Regards

Sancar

--- End Message ---
--- Begin Message ---
On Mon, 2007-12-24 at 12:13 -0500, tedd wrote:
> At 4:18 PM +0200 12/19/07, Sancar Saran wrote:
> >  > that said, avoid globals like the plague - sometimes you may come up with
> >>  a situation where using a global is really necessary - such situations
> >>  should be the exception rather than the rule, often if your thinking of
> >>  using a global there is another way of doing it. jmho
> >
> >And this is why I'm asking here, WHY I should avoid globals like plague ?
> >Every one says bad. Alright, but no one says WHY ?
> 
> Hi:
> 
> I'm a little late to this thread (been busy), but this is why I 
> rarely use globals and have not used them in php.
> 
> One of the fundamentals of programming I've learned is to reduce 
> problem/solution to their most basic form. Simply, you see a problem 
> and you solve it by dividing the problem it into smaller parts and 
> then writing code to solve the small parts. Eventually, all the small 
> solutions come together to solve the larger problem.
> 
> Certainly, and it seems even logical, that you can use a global 
> variable to communicate between the different parts and everything 
> should work -- IF -- that's all there was to it. But, if you have to 
> debug the code OR if you want to use a portion of that solution to 
> solve a different problem, then you can have difficulties. For 
> example:
> 
> [1] If you have to debug the code, there's not a really good way to 
> look at a function and see if a variable in it is a global or not -- 
> therein lies a problem, you don't readily know.
> 
> In other languages I adopted a naming convention that used "g" as the 
> first letter of a Global variable, like gThisIsMyGolbalVariable -- 
> that way at least I knew the variable was global. But, even then I 
> didn't know where the global was defined or where it might be changed 
> -- it was just an unknown in my function that I had to consider.
> 
> [2] If you want to reused a portion of the code, then you have to 
> also accommodate the global. This makes transporting your code from 
> one application to another problematic because you never know IF your 
> function replies on a global or not. If you never use globals, then 
> that's never a problem.
> 
> So, my advice is to not use globals, but rather make the functions 
> independent from globals. That way you troubleshoot them easier and 
> can cut/paste them into other solutions without having to worry if 
> some part of that function relies on something who knows where.
> 
> That's my reasons why I avoid globals.

You're advice isn't very applicable in some cases. I absoltely agree it
is very bad coding style to use globals to communicate information from
one function to another etc. For instance, using globals to facilitate a
database connection is very bad IMHO, the DB connection should be
retrieved from a function or singleton... I've seen this be a very real
problem when I've been asked to merge code from separate projects into a
unified system. Additionally, I generally agree that using the global
keyword and then stealthily using a global variable reduces clarity of
where the variable came from. However, this is mitigated by using the
$GLOBALS struct explicitly. Also, by using the $GLOBALS struct
explicitly there's no need to prefix your variable names with 'g' :)
Although, in JavaScript and in C on the occasion where I've used
globals, I also use a 'g' prefix for clarity. Continuing on though, as I
said in a previous message, using the $GLOBALS array for project
configuration is very convenient and IMHO clean provided that you use a
double level methodology where the first level is the name of your
project or some other unlikely to be clobbered name. For instance,
InterJinn has most of it's framework settings declared via:

    $GLOBALS['interJinn']['someConfigProperty'] = 'someValue';

I find this clean, readable, maintainable, and without the need for a
superfluous solution to handle framework configuration. One could use
the fairly standard .ini convention with key/value pairs, but this is a
pain for arrays, generally requires a caching system once parsed, and
can't even nearly compete with compile cached PHP code :)

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

--- End Message ---
--- Begin Message ---
the following url explains how to build a single extension for an
existing php installation .. it's not really Mac specific and may help
someone on another system stuck with the same problem:

http://lists.apple.com/archives/macos-x-server/2007/Nov/msg00315.html


Jochem Maas schreef:
> hi guys,
> 
> well having tried for countless hours to build php on leopard I pretty much 
> gave up.
> apparently it's pretty much impossible unless your name is Marc Liyanage 
> (entropy.ch) ...
> 
> the problem lies with the fact that you need 64bit libs and the some (most 
> notably iconv)
> of the libs included with Leopard are borked in respect to the 'universal 
> build' stuff (which
> I gather means you actually have a number of different [architecture related] 
> executables bundled
> into a single file ... I probably have that all wrong, to be honest it's a 
> little over my head.
> 
> Marc L. offers a tarball with a working php5.2.5 (just untar and move the 
> php5 dir to /usr/local):
>        http://www2.entropy.ch/download/php5-5.2.5.leopard.release1.tar.gz
> 
> his build does work but it doesn't include one extension that I rely on for 
> some of my projects,
> namely interbase.
> 
> I figured I'd try using Marc' configure line (as given by 
> /usr/local/php5/bin/php-config against the
> source of php5.2.5 that I downloaded and add the relevant configure option 
> (--with-interbase[=DIR])
> ... in the hope I at least get a couple of usable extensions so that I could 
> copy the ibase extension
> over into the working php5.2.5 installation ... no joy.
> 
> I figure I'm screwed - I have a painfully expensive dev machine I can't 
> blooming use. oh well,
> at least it is the nicest looking paper weight I've got.
> 
> I'm still wondering whether it's possible to build just the interbase 
> extension ... anyone know
> how to do that? or have any tips?
> 
> rgds,
> Jochem
> 

--- End Message ---
--- Begin Message --- Just getting into DOMDocument()... I'm loading an HTML page and trying to extract certain bits of text. Just one problem: loadHTML() seems to ignore orphan tags like '<br>'. For example, in the following HTML:

<div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div>

If I run the above HTML through:

$nodes = $table->getElementsByTagName("*");

I only get three nodes that I can iterate through (<div>). What I want to do is split/explode the three lines within each div, but when I look at the nodeValue of each node, it only shows something like "Some text is here. New line. Another new line."

Any ideas?

...Rene

--- End Message ---
--- Begin Message ---
That's because it's not proper XHTML: "<br>" should be "<br />".



On Dec 24, 2007, at 6:03 PM, M5 <[EMAIL PROTECTED]> wrote:

Just getting into DOMDocument()... I'm loading an HTML page and trying to extract certain bits of text. Just one problem: loadHTML() seems to ignore orphan tags like '<br>'. For example, in the following HTML:

<div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div>

If I run the above HTML through:

$nodes = $table->getElementsByTagName("*");

I only get three nodes that I can iterate through (<div>). What I want to do is split/explode the three lines within each div, but when I look at the nodeValue of each node, it only shows something like "Some text is here. New line. Another new line."

Any ideas?

...Rene

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
Actually, never mind. It does not have to be valid to work.



On Dec 24, 2007, at 6:15 PM, Casey <[EMAIL PROTECTED]> wrote:

That's because it's not proper XHTML: "<br>" should be "<br />".



On Dec 24, 2007, at 6:03 PM, M5 <[EMAIL PROTECTED]> wrote:

Just getting into DOMDocument()... I'm loading an HTML page and trying to extract certain bits of text. Just one problem: loadHTML () seems to ignore orphan tags like '<br>'. For example, in the following HTML:

<div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div>

If I run the above HTML through:

$nodes = $table->getElementsByTagName("*");

I only get three nodes that I can iterate through (<div>). What I want to do is split/explode the three lines within each div, but when I look at the nodeValue of each node, it only shows something like "Some text is here. New line. Another new line."

Any ideas?

...Rene

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message --- OK, I already knew that making it valid doesn't change the result. But the question remains, how to parse the HTML as it arrives (which I have no control over anyway), besides doing a str_replace on <br> and inserting a token, which I later replace (which I shouldn't have to, right?)

...Rene


On 24-Dec-07, at 7:19 PM, Casey wrote:

Actually, never mind. It does not have to be valid to work.



On Dec 24, 2007, at 6:15 PM, Casey <[EMAIL PROTECTED]> wrote:

That's because it's not proper XHTML: "<br>" should be "<br />".



On Dec 24, 2007, at 6:03 PM, M5 <[EMAIL PROTECTED]> wrote:

Just getting into DOMDocument()... I'm loading an HTML page and trying to extract certain bits of text. Just one problem: loadHTML () seems to ignore orphan tags like '<br>'. For example, in the following HTML:

<div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div> <div class="text">Some text is here. <br> New line. <br> Another new line. </div>

If I run the above HTML through:

$nodes = $table->getElementsByTagName("*");

I only get three nodes that I can iterate through (<div>). What I want to do is split/explode the three lines within each div, but when I look at the nodeValue of each node, it only shows something like "Some text is here. New line. Another new line."

Any ideas?

...Rene

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message --- I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines:

Calgary, AB  T2A6C1
Toronto, ON     T4M 0B0
Saint John,  NB    E2L 4L1

...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.:

Array
(
        [city]  => "Calgary",
        [prov]  => "AB",
        [postal]        => "T2A 6C1"
)

Array
(
        [city]  => "Toronto",
        [prov]  => "ON",
        [postal]=> "T4M 0B0"
)

Array
(
        [city]  => "Saint John",
        [prov]  => "NB",
        [postal]=> "E2L 4L1"
)

--- End Message ---
--- Begin Message ---
On Dec 24, 2007, at 7:34 PM, M5 <[EMAIL PROTECTED]> wrote:

I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines:

Calgary, AB  T2A6C1
Toronto, ON     T4M 0B0
Saint John,  NB    E2L 4L1

...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.:

Array
(
   [city]    => "Calgary",
   [prov]    => "AB",
   [postal]    => "T2A 6C1"
)

Array
(
   [city]    => "Toronto",
   [prov]    => "ON",
   [postal]=> "T4M 0B0"
)

Array
(
   [city]    => "Saint John",
   [prov]    => "NB",
   [postal]=> "E2L 4L1"
)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Try this:
$places = array();
$lines = explode("\n", $toparse);
foreach ($lines as $i => $line)
list($places[$i]['city'], $places[$i]['prov'], $places[$i] ['postal']) = explode(' ', $line, 3);'
--- End Message ---
--- Begin Message ---
On Dec 24, 2007, at 7:59 PM, Casey <[EMAIL PROTECTED]> wrote:

On Dec 24, 2007, at 7:34 PM, M5 <[EMAIL PROTECTED]> wrote:

I'm learning regular expressions, and trying to figure out what's possible and what's not. Any ideas of how to create a preg_match expression to parse following three lines:

Calgary, AB  T2A6C1
Toronto, ON     T4M 0B0
Saint John,  NB    E2L 4L1

...such that it splits each line into City, Province and Postalcode (irrespective of occasional white space), e.g.:

Array
(
  [city]    => "Calgary",
  [prov]    => "AB",
  [postal]    => "T2A 6C1"
)

Array
(
  [city]    => "Toronto",
  [prov]    => "ON",
  [postal]=> "T4M 0B0"
)

Array
(
  [city]    => "Saint John",
  [prov]    => "NB",
  [postal]=> "E2L 4L1"
)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Try this:
$places = array();
$lines = explode("\n", $toparse);
foreach ($lines as $i => $line)
list($places[$i]['city'], $places[$i]['prov'], $places[$i] ['postal']) = explode(' ', $line, 3);'
I'm very sorry about that, Ive been wrong all week!

It doesn't parse Saint John correctly :(

--- End Message ---
--- Begin Message ---
M5 schreef:
> I'm learning regular expressions, and trying to figure out what's
> possible and what's not. 

pretty much anything as far as string parsing goes.

> Any ideas of how to create a preg_match
> expression to parse following three lines:

yes. given your intention to learn regexps why not try to come
up with one? if you get stuck show us what you have so far ...
you don't learn to eat with cutlery by being spoonfed.

if you want to parse the whole string in one go you'll want to use
preg_match_all() otherwise use preg_match and iterate over each line ..

foreach(explode("\n", $data) as $line) {
        $matches = array();
        if (preg_match($line, $regexp, $matches)) {
                /* do something */
        }
}

> 
> Calgary, AB  T2A6C1
> Toronto, ON     T4M 0B0
> Saint John,  NB    E2L 4L1
> 
> ...such that it splits each line into City, Province and Postalcode
> (irrespective of occasional white space), e.g.:
> 
> Array
> (
>     [city]    => "Calgary",
>     [prov]    => "AB",
>     [postal]    => "T2A 6C1"
> )
> 
> Array
> (
>     [city]    => "Toronto",
>     [prov]    => "ON",
>     [postal]=> "T4M 0B0"
> )
> 
> Array
> (
>     [city]    => "Saint John",
>     [prov]    => "NB",
>     [postal]=> "E2L 4L1"
> )
> 

--- End Message ---

Reply via email to