php-general Digest 5 Jun 2008 12:18:11 -0000 Issue 5498
Topics (messages 275032 through 275054):
Quickly verifying single word.
275032 by: Tyson Vanover
275034 by: Ted Wood
275036 by: Shawn McKenzie
275037 by: Shawn McKenzie
275042 by: Per Jessen
275047 by: Usamah M. Ali
275048 by: Usamah M. Ali
275050 by: Per Jessen
Re: Objects and Traversing
275033 by: Ted Wood
275035 by: Jim Lucas
Re: Regex in PHP
275038 by: Nathan Nobbe
275039 by: Robert Cummings
275040 by: Nathan Nobbe
275041 by: Robert Cummings
275049 by: Richard Heyes
Are there free http mysql tunneling writed in php ?
275043 by: KLEIN Stéphane
275044 by: Per Jessen
275045 by: Colin Guthrie
mail() error handling
275046 by: Henrik Johansson
275051 by: Stut
cancel of <[EMAIL PROTECTED]>
275052 by: Henrik Johansson
Re: A problem with fgets()
275053 by: Usamah M. Ali
Dynamic form changes without refresh
275054 by: Mayer, Jonathan
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 ---
I need a quick way to make sure that a string is a
single word with no white spaces. I would prefer that
it is a command that could fit on a single line. Or at
least an if block.
I have a few thoughts on this but it involves things
like explode(), stripslashes(), etc.
--- End Message ---
--- Begin Message ---
There's probably a regex solution that is most elegant, but here is
one solution:
if ($str == str_replace(array(' ', "\n"), '', $str)) {
// if you get here, then $str has no spaces or newline characters
}
~Ted
On 4-Jun-08, at 4:04 PM, Tyson Vanover wrote:
I need a quick way to make sure that a string is a single word with
no white spaces. I would prefer that it is a command that could fit
on a single line. Or at least an if block.
I have a few thoughts on this but it involves things like explode(),
stripslashes(), etc.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
Tyson Vanover wrote:
I need a quick way to make sure that a string is a single word with no
white spaces. I would prefer that it is a command that could fit on a
single line. Or at least an if block.
I have a few thoughts on this but it involves things like explode(),
stripslashes(), etc.
if (strpos($string, ' ') === false) {
//may not work for tabs, newlines etc
echo 'No spaces!';
}
if (preg_match('/[\s]*/', $string) === false) {
echo 'No spaces!';
}
-Shawn
--- End Message ---
--- Begin Message ---
Shawn McKenzie wrote:
Tyson Vanover wrote:
I need a quick way to make sure that a string is a single word with no
white spaces. I would prefer that it is a command that could fit on a
single line. Or at least an if block.
I have a few thoughts on this but it involves things like explode(),
stripslashes(), etc.
if (strpos($string, ' ') === false) {
//may not work for tabs, newlines etc
echo 'No spaces!';
}
if (preg_match('/[\s]*/', $string) === false) {
echo 'No spaces!';
}
-Shawn
Second one doesn't work for some reason. No time now to test, will later.
-Shawn
--- End Message ---
--- Begin Message ---
Shawn McKenzie wrote:
>> if (preg_match('/[\s]*/', $string) === false) {
>> echo 'No spaces!';
>> }
>>
>> -Shawn
>
> Second one doesn't work for some reason. No time now to test, will
> later.
How about:
if (preg_match('/\s/', $string) === false) {
echo 'No spaces!';
}
/Per Jessen, Zürich
--- End Message ---
--- Begin Message ---
On Thu, Jun 5, 2008 at 9:19 AM, Per Jessen <[EMAIL PROTECTED]> wrote:
> Shawn McKenzie wrote:
>
>>> if (preg_match('/[\s]*/', $string) === false) {
>>> echo 'No spaces!';
>>> }
>>>
>>> -Shawn
>>
>> Second one doesn't work for some reason. No time now to test, will
>> later.
>
> How about:
>
> if (preg_match('/\s/', $string) === false) {
> echo 'No spaces!';
> }
>
>
Won't work either. The problem lies in using the === comparison
operator. preg_match() returns 0 if no match is found, and FALSE if an
error occurred. So using === will always echo 'No spaces' whether
there were spaces or not, provided that no error has occurred.
if (!preg_match('/\s+/i', $string))
{
echo 'No spaces';
}
should suffice.
Regards,
Usamah
--- End Message ---
--- Begin Message ---
On Thu, Jun 5, 2008 at 12:02 PM, Usamah M. Ali <[EMAIL PROTECTED]> wrote:
> Won't work either. The problem lies in using the === comparison
> operator. preg_match() returns 0 if no match is found, and FALSE if an
> error occurred. So using === will always echo 'No spaces' whether
> there were spaces or not, provided that no error has occurred.
>
> if (!preg_match('/\s+/i', $string))
> {
> echo 'No spaces';
> }
>
> should suffice.
>
> Regards,
> Usamah
>
Won't work either. :)
This should work fine:
if (preg_match('|\s+|i', $string) === 0)
{
echo '$string does not contain white spaces!';
}
elseif preg_match('/\s+/i', $string)
{
echo 'One or more white spaces found!';
}
else
{
echo 'An error has occurred!';
}
According to your needs, it could be simply shortened to the first if:
if (preg_match('/\s+/i', $string) === 0)
{
echo 'No white spaces';
}
Hope that works well.
Regards,
Usamah
--- End Message ---
--- Begin Message ---
Usamah M. Ali wrote:
> According to your needs, it could be simply shortened to the first if:
> if (preg_match('/\s+/i', $string) === 0)
> {
> echo 'No white spaces';
> }
And you could even remove the '+'.
/Per Jessen, Zürich
--- End Message ---
--- Begin Message ---
You should be able to access the "Name" field using this syntax:
QueryResult->records[0]->sobjects[0]->fields->Name
Reading from left-to-right:
1. accessing index 0 (zero) of the "records" array.
2. accessing index 0 (zero) of the "objects" array.
3. accessing the "Name" property of the "fields" SimpleXML element
A more verbose approach would be:
$fld = $queryObject->records; // get the "records" array
$fld = $fld[0]; // get the first index of the array
$fld = $fld->sobjects; // get the "sobjects" array
$fld = $fld[0]; // get the first index of the array
$fld = $fld->fields; // get the "fields" SimpleXML element
$fld = $fld->Name; // get the Name property
Either way, you're "walking" left-to-right, top-to-bottom.
~Ted
On 4-Jun-08, at 3:37 PM, VamVan wrote:
Hello Guys,
Here is the object. How can I get access to the [Name] = access.
People who
help me with this can you please tell me what is the logic behind
traversing
the objects and how do you gather the values. Thanks. I am looking
for an
answer like this $queryResult->f....->dsfsdf
QueryResult Object
(
[queryLocator] =>
[done] => 1
[records] => Array
(
[0] => SObject Object
(
[type] => Contact
[fields] =>
[sobjects] => Array
(
[0] => SObject Object
(
[type] => Account
[fields] => SimpleXMLElement Object
(
[Name] => Access
)
)
)
)
)
[size] => 1
)
--- End Message ---
--- Begin Message ---
VamVan wrote:
Hello Guys,
Here is the object. How can I get access to the [Name] = access. People who
help me with this can you please tell me what is the logic behind traversing
the objects and how do you gather the values. Thanks. I am looking for an
answer like this $queryResult->f....->dsfsdf
QueryResult Object
(
[queryLocator] =>
[done] => 1
[records] => Array
(
[0] => SObject Object
(
[type] => Contact
[fields] =>
[sobjects] => Array
(
[0] => SObject Object
(
[type] => Account
[fields] => SimpleXMLElement Object
(
[Name] => Access
)
)
)
)
)
[size] => 1
)
Simply this would give you name
$QueryResult->records[0]->sobjects[0]->fields->Name
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--- End Message ---
--- Begin Message ---
On Wed, Jun 4, 2008 at 2:06 PM, Robert Cummings <[EMAIL PROTECTED]>
wrote:
> On Wed, 2008-06-04 at 11:18 -0600, Nathan Nobbe wrote:
> > On Wed, Jun 4, 2008 at 11:12 AM, Robert Cummings <[EMAIL PROTECTED]>
> > wrote:
> >
> > > Did you just try to use a test that used a single iteration to prove me
> > > wrong? OMFG ponies!!! Loop each one of those 10 million times, use a
> > > separate script for each, and use the system time program to
> > > appropriately measure the time the system takes.
> >
> >
> > <?php
> >
> > $str = 'asSAFAASFDADSfasfjhalskfjhlaseAERQWERQWER;.dafasjhflasfjd';
> > $search = 'fdasASDFAafdas';
> >
> > $start = microtime();
> >
> > for($i = 0; $i < 10000000; $i++)
> > strpos($str, $search);
> >
> > $end = microtime();
> > $r1 = $end - $start;
> >
> > $start = microtime();
> >
> > for($i = 0; $i < 10000000; $i++)
> > stripos($str, $search);
> >
> > $end2 = microtime();
> > $r2 = $end2 - $start;
> >
> > echo "strpos: $r1\n";
> > echo "stripos: $r2\n";
> >
> > if($r2 < $r1) {
> > echo 'stripos is faster' . PHP_EOL;
> > }
> > ------------------------------
> > strpos: 0.730519
> > stripos: -0.098887
> > stripos is faster
>
> Negative time eh!? You're code must be buggy :| The time program works
> like this unde rmost nix systems:
>
> time php -q foo.php
>
> And then it returns a report of how much time was taken for various
> types of time. I've already sent an email with the appropriate timing of
> both versions. BTW, as primtive as microtime() is for this kind of
> measurement... you might want to read the manual to use it properly:
>
> http://ca3.php.net/manual/en/function.microtime.php
>
> You probably want:
>
> microtime( true )
>
> > stripos still dominates ;) what is this system time program you speak of
> ?
> > and, ill put them into separate programs when i get home this evening,
> and
> > have more time to screw around.
>
> It's a simple thought process to understand that unless someone coding
> the PHP internals buggered their code, that stripos() cannot possibly be
> faster than strpos(). I really don't need benchmarks for something this
> simple to know which SHOULD be faster.
>
i repeated your test using the time program and splitting the script into 2,
one for each strpos and stripos, to find similar results. imo, there is no
need for 2 comparisons for case-insensitive searches, because both arguments
can be converted to a single case prior to the search. obviously, there is
a small amount of overhead there the case-sensitive search is unencumbered
by. i guess i never sat down and thought about how that algorithm would
work (case-sensitive) =/.
thanks for the tips rob. sorry to bother you richard.
-nathan
--- End Message ---
--- Begin Message ---
On Wed, 2008-06-04 at 23:20 -0400, Nathan Nobbe wrote:
>
> i repeated your test using the time program and splitting the script into 2,
> one for each strpos and stripos, to find similar results. imo, there is no
> need for 2 comparisons for case-insensitive searches, because both arguments
> can be converted to a single case prior to the search. obviously, there is
> a small amount of overhead there the case-sensitive search is unencumbered
> by. i guess i never sat down and thought about how that algorithm would
> work (case-sensitive) =/.
>
> thanks for the tips rob. sorry to bother you richard.
You would do two comparisons... why incur the overhead of a conversion
if one is not necessary. First you do case sensitive match, if that
fails then you try the alternative version comparison. It is inefficient
to perform 2 conversions and a single comparison in contrast. Similarly,
it's very inefficient to convert two entire strings then perform a
comparison. If the first characters differ then conversion of the rest
of the strings was pointless. This is basic algorithms in computer
science.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
On Wed, Jun 4, 2008 at 11:43 PM, Robert Cummings <[EMAIL PROTECTED]>
wrote:
> On Wed, 2008-06-04 at 23:20 -0400, Nathan Nobbe wrote:
> >
> > i repeated your test using the time program and splitting the script into
> 2,
> > one for each strpos and stripos, to find similar results. imo, there is
> no
> > need for 2 comparisons for case-insensitive searches, because both
> arguments
> > can be converted to a single case prior to the search. obviously, there
> is
> > a small amount of overhead there the case-sensitive search is
> unencumbered
> > by. i guess i never sat down and thought about how that algorithm would
> > work (case-sensitive) =/.
> >
> > thanks for the tips rob. sorry to bother you richard.
>
> You would do two comparisons... why incur the overhead of a conversion
> if one is not necessary.
because it simplifies the algorithm, there is no need for conditional logic.
> First you do case sensitive match, if that
> fails then you try the alternative version comparison. It is inefficient
> to perform 2 conversions and a single comparison in contrast.
3 operations vs. 1 or potentially 2, sure.
> Similarly,
> it's very inefficient to convert two entire strings then perform a
> comparison.
then they could be converted one at a time as the strings were traversed to
increase efficiency.
> If the first characters differ then conversion of the rest
> of the strings was pointless.
good point.
> This is basic algorithms in computer
> science.
>
you really know how to rub it in there rob. but i was looking at the
implementation in the php code, looks like somebody likes my idea (this code
found in ext/standard/string.c). on the second line the haystack is
converted to lower case[1], then if it passes a couple of checks, the needle
is converted to lower case[2], and lastly the comparison is performed[3].
there is no logic to check both cases.
(i have placed a star beside the statements ive referred to).
...
haystack_dup = estrndup(haystack, haystack_len);
*[1] php_strtolower(haystack_dup, haystack_len);
if (Z_TYPE_P(needle) == IS_STRING) {
if (Z_STRLEN_P(needle) == 0 || Z_STRLEN_P(needle) > haystack_len) {
efree(haystack_dup);
RETURN_FALSE;
}
needle_dup = estrndup(Z_STRVAL_P(needle), Z_STRLEN_P(needle));
*[2] php_strtolower(needle_dup, Z_STRLEN_P(needle));
*[3] found = php_memnstr(haystack_dup + offset, needle_dup,
Z_STRLEN_P(needle), haystack_dup + haystack_len);
}
...
-nathan
--- End Message ---
--- Begin Message ---
On Thu, 2008-06-05 at 00:24 -0400, Nathan Nobbe wrote:
>
> you really know how to rub it in there rob. but i was looking at the
> implementation in the php code, looks like somebody likes my idea
> (this code
> found in ext/standard/string.c). on the second line the haystack is
> converted to lower case[1], then if it passes a couple of checks, the
> needle
> is converted to lower case[2], and lastly the comparison is
> performed[3].
> there is no logic to check both cases.
> (i have placed a star beside the statements ive referred to).
> ...
> haystack_dup = estrndup(haystack, haystack_len);
> *[1] php_strtolower(haystack_dup, haystack_len);
>
> if (Z_TYPE_P(needle) == IS_STRING) {
> if (Z_STRLEN_P(needle) == 0 || Z_STRLEN_P(needle) >
> haystack_len) {
> efree(haystack_dup);
> RETURN_FALSE;
> }
>
> needle_dup = estrndup(Z_STRVAL_P(needle), Z_STRLEN_P(needle));
> *[2] php_strtolower(needle_dup, Z_STRLEN_P(needle));
> *[3] found = php_memnstr(haystack_dup + offset, needle_dup,
> Z_STRLEN_P(needle), haystack_dup + haystack_len);
> }
Funny, I guess they took the quick route. This code could obviously be
optmized :)
But let's go with something used more often... such as more traditional
string comparison where you're more likely to want to eke out
efficiency:
ZEND_API int zend_binary_strcasecmp(char *s1, uint len1, char *s2, uint
len2)
{
int len;
int c1, c2;
len = MIN(len1, len2);
while (len--) {
c1 = zend_tolower((int)*(unsigned char *)s1++);
c2 = zend_tolower((int)*(unsigned char *)s2++);
if (c1 != c2) {
return c1 - c2;
}
}
return len1 - len2;
}
Well looks like they do indeed do a conversion.. but on a char by char
basis. Strange that. Could more than likely speed it up by doing an
initial exactness comparison and then falling back on the above. Maybe
I'll compile and test out the following later:
ZEND_API int zend_binary_strcasecmp
(char *s1, uint len1, char *s2, uint len2)
{
int len;
int c1, c2;
len = MIN(len1, len2);
while (len--) {
c1 = (int)*(unsigned char *)s1++;
c2 = (int)*(unsigned char *)s2++;
if( c1 != c2 ){
c1 = zend_tolower( c1 );
c2 = zend_tolower( c2 );
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
> sorry to bother you richard.
You didn't, I just wanted to make sure I wasn't losing it (more).
--
Richard Heyes
+----------------------------------------+
| Access SSH with a Windows mapped drive |
| http://www.phpguru.org/sftpdrive |
+----------------------------------------+
--- End Message ---
--- Begin Message ---
Hi,
do you know one free (open source) http mysql tunneling writed in php ?
Thanks for your information,
Stephane
--- End Message ---
--- Begin Message ---
KLEIN Stéphane wrote:
> Hi,
>
> do you know one free (open source) http mysql tunneling writed in php
> ?
I don't know of any specific implementation, but surely you can write
one in about 60 seconds.
get http://domain/run-this-sql?db=database&text=blahblahblah
I'm sure you get the idea.
/Per Jessen, Zürich
--- End Message ---
--- Begin Message ---
KLEIN Stéphane wrote:
do you know one free (open source) http mysql tunneling writed in php ?
Do you mean tunnelling in the classic sense, e.g. have a webpage that
spits reads in data and spits out data and to which you can effectively
connect a standard mysql client to?
Or do you just mean a generic interact to mysql written in PHP?
If the former, I don't know. If the latter PHPMyAdmin is your friend.
Col
--- End Message ---
--- Begin Message ---
Hi.
I have a piece of code that uses mail() to, well, send mail.
It works, but not the error handling which is as follows:
<snip>
$send = @mail( $to, $subject, $body, $headers );
if($send)
{header( "Location: http://somewhere/thankyou.html" );}
else
{print "Oops, couldn't deliver the message, please try again soon."; }
I checked, and it always returns 1, even though I tried a [EMAIL PROTECTED] that
doesn't exist!
Anyone got a clue what I've done wrong?
Kind Regards,
Henrik Johansson
System Administrator
RADIUS Sweden AB
--- End Message ---
--- Begin Message ---
On 5 Jun 2008, at 09:48, Henrik Johansson wrote:
I have a piece of code that uses mail() to, well, send mail.
It works, but not the error handling which is as follows:
<snip>
$send = @mail( $to, $subject, $body, $headers );
if($send)
{header( "Location: http://somewhere/thankyou.html" );}
else
{print "Oops, couldn't deliver the message, please try again soon."; }
I checked, and it always returns 1, even though I tried a
[EMAIL PROTECTED] that
doesn't exist!
Anyone got a clue what I've done wrong?
The mail function will succeed if it completes it's task, which on
unix-based systems is simply to pass the message on to sendmail. So,
in your case I'd say sendmail is accepting the message so mail is
quite correct in thinking it succeeded.
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
cancel by original author
--- End Message ---
--- Begin Message ---
Well, finally found an explanation and a solution: calling fgets()
twice. Because if there's no length limit supplied (and it's not EOF)
when calling the function, it continues reading until it finds the
first newline character. So calling fgets() again will ensure that it
will read from the _beginning_ of the *next* line:
<?php
function getCity($file)
{
// Try to open the file
if (!file_exists($file) || !($handle = fopen($file, "r")))
{
die('Could not open file for reading.');
}
$size = filesize($file);
$randval = rand(0, $size);
// Seek to a random position in the file
fseek($handle, $randval);
// Get random city name
$line = trim(fgets($fp, 2048));
$line = trim(fgets($fp, 2048));
fclose($fp);
// If line was empty, try again
if(empty($line))
{
$line = getCity($file);
}
return($line);
}
?>
To clarify more, if the first call to fgets() reads the line
containing the city name 'Alexandria', it might be read as:
andria
Now calling fgets() again will definitely read the whole next line:
Dallas
>
> Yes, I agree that that note is complete hogwash, and have just deleted
> it!
>
> Cheers!
>
> Mike
>
Thanks. :)
--- End Message ---
--- Begin Message ---
Hiya all,
I have a PHP page with a form holding a large number of textarea boxes.
These text boxes are individually set as enabled or disabled based on a flag
held in a MySQL database.
I want to be able to dynamically enable or disable a specific box without
the page being recalled (potentially loosing the parameters passed, and
having to rebuild a large complex table).
Using some basic javascript/AJAX, I have set up a link next to each text
box, which calls code that updates the database in the background without
refreshing the page. If I manually refresh the page, the textarea box in
question updates correctly, proving that the database update has worked.
However, I cannot work out how to get the textarea boxes to toggle the
disable on/off "on the fly". I assume they would need to continually check
the database and adjust as required.
I realise there may not be too much PHP involved here, but I was wondering
whether anyone could assist or point me in the right direction.
Thanks,
Jon.
--- End Message ---