RE: [PHP] eval();

2005-10-30 Thread Josh McDonald
Keep in mind, eval()ing code you pull from the database will also raise the
damage from a SQL injection attack or similar from a PITA
restore-your-database to a much bigger PITA format-webserver.

-Josh
 
--
 
My name was Brian McGee
I stayed up listening to Queen
When I was seventeen

  Josh 'G-Funk' McDonald  ::  Pirion Systems, Brisbane
 
 07 3257 0490  ::  0437 221 380  ::  [EMAIL PROTECTED]
 

-Original Message-
From: Richard Lynch [mailto:[EMAIL PROTECTED] 
Sent: Monday, 31 October 2005 3:57 PM
To: John Taylor-Johnston
Cc: php-general@lists.php.net; Jasper Bryant-Greene
Subject: Re: [PHP] eval();

On Sun, October 30, 2005 8:51 pm, John Taylor-Johnston wrote:
> eval( " ?> $contents 
>>However, if eval() is the answer, you're probably asking the wrong 
>>question. You should take a hard look at your code and think of a  
>>better way to do what you need to do.
>>
>>
> Back to the drawing board? It is either store my html+embedded code in 
> a mysql record, or in an html file, which means playing with fopen. 
> It's easier to hand tweak in phpmyadmin.
> Nonetheless, even though your test code worked (thanks!) this doesn't.
> Sigh.
>
> if ($contents = displaynew()){
echo "CONTENTS:", htmlentities($contents), "\n";  eval( " ?>
$contents 
> function displaynew()
> {
>$file = basename($_SERVER['PHP_SELF']);
>require 'connect.inc';
>$sql = "SELECT HTML FROM `$db`.`$table_editor` WHERE `Filename` 
> LIKE '".addslashes($file)."' LIMIT 1;";
>if ($myquery = mysql_query($sql) and mysql_num_rows($myquery) > 0)

This 'and' should probably be '&&' ...

Though I never really used 'and' enough to know for sure.

At any rate, you've got *NO* error-checking for an invalid query here.

> {
>$mydata = mysql_fetch_array($myquery, MYSQL_NUM);
>return $mydata[0];
>}
>return false;
> }


--
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] [DONE] Substr by words

2005-10-30 Thread Richard Lynch
On Sun, October 30, 2005 6:04 am, Marcus Bointon wrote:
> On 29 Oct 2005, at 20:41, Richard Lynch wrote:
>
>> It was probably replacing *TWO* spaces with one.
>>
>> If so, it should really be in a while loop, because there could be 3
>> or more spaces in a row, and if the goal is only single-spaced
>> words...
>
> I can hardly think of a better application for a regex:
>
> $text = preg_replace('/  */', ' ', $text);

Sure.

Now you wanna go re-write all my code that pre-dates preg_* functions
being added to PHP? :-)

Un-paid? :-) :-) :-)

Cuz I probably still do have a while(ststr(...)) str_replace() loop or
two in there somewhere from PHP3.0rc2 days...

Yes, ereg_* were in then.

But they were kinda slow and I didn't undestand Regex then.

Hell, I barely understand it now, really.

There's a certain point where the Regex expression reaches a level of
complexity that I'm just not willing to accept in my code and call it
"maintainable"

/  */ is fine, of course.

But there's lots of times when I know there must be a one-line regex
to replace 10 lines of code, but I don't WANT to use it because I'll
stumble over that one-line Regex every time I have to change it.

-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] Type of form element

2005-10-30 Thread Richard Lynch
On Sun, October 30, 2005 5:52 am, Marcus Bointon wrote:
> On 29 Oct 2005, at 20:59, Richard Lynch wrote:
>
>> So you will most likely be using isset($_POST['checkbox_name'])
>> rather
>> than testing for "on"
>
> I classify using isset for checking for the existence of array keys
> to be a bad habit as in some common cases it will not work as you
> expect, for example:
>
> 

A)
If you do not specify a "value=" in HTML for a checkbox, the value is
"on" by default.

So this should give you checkbox_name=on in a GET form.

If it doesn't, your browser is very very very broken.
[Probably Internet Explorer would be the one to get this wrong, if any
of them do]


B)
I dunno what version of PHP you are using, nor what php.ini settings
you have, but, at least in my version/settings:

http://example.com/test.php?checkbox_name=
isset($_GET['checkbox_name']) returns 1

Since HTTP and HTML have no concept of NULL, I'm not real worried that
$_GET will have NULL values in it.

I am certainly not going to pollute $_GET/$_POST/$_REQUEST by stuffing
NULL values into them!

If you want to do things that way, go right ahead.

But I really do believe isset($_POST['checkbox_name']) is a "good"
coding practice.

DSFDD

-- 
Like Music?
http://l-i-e.com/artists.htm


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



Re: [PHP] eval();

2005-10-30 Thread Richard Lynch
On Sun, October 30, 2005 8:51 pm, John Taylor-Johnston wrote:
> eval( " ?> $contents 
>>However, if eval() is the answer, you're probably asking the wrong
>>question. You should take a hard look at your code and think of a
>> better
>>way to do what you need to do.
>>
>>
> Back to the drawing board? It is either store my html+embedded code in
> a
> mysql record, or in an html file, which means playing with fopen. It's
> easier to hand tweak in phpmyadmin.
> Nonetheless, even though your test code worked (thanks!) this doesn't.
> Sigh.
>
> if ($contents = displaynew()){
echo "CONTENTS:", htmlentities($contents), "\n";
 eval( " ?> $contents 
> function displaynew()
> {
>$file = basename($_SERVER['PHP_SELF']);
>require 'connect.inc';
>$sql = "SELECT HTML FROM `$db`.`$table_editor` WHERE `Filename`
> LIKE
> '".addslashes($file)."' LIMIT 1;";
>if ($myquery = mysql_query($sql) and mysql_num_rows($myquery) > 0)

This 'and' should probably be '&&' ...

Though I never really used 'and' enough to know for sure.

At any rate, you've got *NO* error-checking for an invalid query here.

> {
>$mydata = mysql_fetch_array($myquery, MYSQL_NUM);
>return $mydata[0];
>}
>return false;
> }


-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] Re: Substr by words

2005-10-30 Thread Gustavo Narea

Hello, Marcus.

Marcus Bointon wrote:

On 30 Oct 2005, at 15:35, Gustavo Narea wrote:

I think that trim($matches[0]) will return the whole string with no  
change.
No, it will return the entire matching pattern, not just the sub- 
matches. I added the trim to remove any leading space, and there will  
nearly always be a trailing space because of the part of my pattern  
that defines a word will include it. It was simpler to use trim than  to 
make the pattern skip it. Did you actually try it?
No. I said that I was not that sure about this because I have not used 
preg_* functions yet.



On the other hand, I think we have to place a caret after the first  
slash.
Only if you insist that your string must start with a word - putting  a 
^ at the start would make it omit the first word if there was a  space 
in front if it.
I think It is OK what I said about the caret, but what we need to change 
is the position of \W*:

   Your suggestion: /(\b\w+\b\W*){1,$MaxWords}/
   My suggestion: /^(\W*\b\w+\b){1,$MaxWords}/

We need the *first* ($MaxWords)th words.


Instead of preg_match(), I had to type preg_replace():
err. I think you missed the point here. You don't need all that messy  
substr stuff at all. The preg_match already did it.


Sorry, you are right. Maybe I thought I was talking about the former 
script I suggested...


What do you think if we use the script you suggested, but we change the 
regex to what I said above? It will look like:


$MyOriginalString = "This is my original string.\nWhat do you think 
about this script?";

$MaxWords = 6; // How many words are needed?
$matches = array();
if (preg_match("/^(\W*\b\w+\b){1,$MaxWords}/", $MyOriginalString, 
$matches)) {

$result = trim($matches[0]);
echo $result;
}
?>

Best regards,

Gustavo Narea.

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



Re: [PHP] un-eval()

2005-10-30 Thread John Taylor-Johnston

I guess I won the powerball $100 million. I'd feel better with the cash ;)
John
Robert Cummings wrote:


Does eval() have a synonym?
The infinite set of source code permutations that can output the word
'red' as a sole output regardless of whatever other logic may occur
within the content of $code. I use 'can output' because the particular
evaluation may not always output 'red' since the above example may be an
isolated case that occurs once every trillion runs or so and we just
happened to hit it :B
 



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



Re: [PHP] eval();

2005-10-30 Thread John Taylor-Johnston

eval( " ?> $contents 
However, if eval() is the answer, you're probably asking the wrong
question. You should take a hard look at your code and think of a better
way to do what you need to do.
 

Back to the drawing board? It is either store my html+embedded code in a 
mysql record, or in an html file, which means playing with fopen. It's 
easier to hand tweak in phpmyadmin.

Nonetheless, even though your test code worked (thanks!) this doesn't. Sigh.

if ($contents = displaynew()) eval( " ?> $contents   $sql = "SELECT HTML FROM `$db`.`$table_editor` WHERE `Filename`  LIKE 
'".addslashes($file)."' LIMIT 1;";

  if ($myquery = mysql_query($sql) and mysql_num_rows($myquery) > 0) {
  $mydata = mysql_fetch_array($myquery, MYSQL_NUM);
  return $mydata[0];
  }
  return false;
}

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



Re: [PHP] un-eval()

2005-10-30 Thread Robert Cummings
On Sun, 2005-10-30 at 21:15, Chris Shiflett wrote:
> John Taylor-Johnston wrote:
> > Does eval() have a synonym?
> 
> I don't usually like to answer a question with a question, but consider 
> this:
> 
> eval($code);
> 
> This produces the following output:
> 
> red
> 
> Given this, what was the value of $code?

The infinite set of source code permutations that can output the word
'red' as a sole output regardless of whatever other logic may occur
within the content of $code. I use 'can output' because the particular
evaluation may not always output 'red' since the above example may be an
isolated case that occurs once every trillion runs or so and we just
happened to hit it :B

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

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



Re: [PHP] eval();

2005-10-30 Thread Jasper Bryant-Greene
On Sun, 2005-10-30 at 21:24 -0500, John Taylor-Johnston wrote:
>  
> >>$contents = " >>arial,helvetica; font-weight: bold;\">About the Project
> >>";
> >>echo eval($contents);
> >>?>
> >>eval() expects PHP code, not HTML with embedded PHP code. Try this
> >>(untested):
> >>
> >>eval( " ?> $contents  >>
> Scary. But it works.
> Shouldn't that error because of the unexpected ?> and  example?

No, because eval(), when passed string $str, effectively does this
silently:

$str = "";

and then passes $str to the PHP interpreter.

Which is, in most cases, what you want, because you want to evaluate PHP
code.

However, if eval() is the answer, you're probably asking the wrong
question. You should take a hard look at your code and think of a better
way to do what you need to do.

I can assure you one does exist, but with your example it's fairly
obvious, so you'd need to explain better what you're trying to do before
I could help you.

-- 
Jasper Bryant-Greene
General Manager
Album Limited

e: [EMAIL PROTECTED]
w: http://www.album.co.nz/
b: http://jbg.name/
p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303
a: PO Box 579, Christchurch 8015, New Zealand

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



Re: [PHP] eval();

2005-10-30 Thread John Taylor-Johnston

$contents = "arial,helvetica; font-weight: bold;\">About the Project

";
echo eval($contents);
?>
eval() expects PHP code, not HTML with embedded PHP code. Try this
(untested):

eval( " ?> $contents


Scary. But it works.
Shouldn't that error because of the unexpected ?> and example?

John

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



Re: [PHP] eval();

2005-10-30 Thread Jasper Bryant-Greene
On Sun, 2005-10-30 at 21:16 -0500, John Taylor-Johnston wrote:
> I need to generate embedded php within in a mysql record. $contents is 
> the actual contents of that record.
> 
>  $contents = " arial,helvetica; font-weight: bold;\">About the Project
> ";
> echo eval($contents);
> ?>
> 
> I get this error:
> Parse error: parse error in /var/www/html2/test.php(4) : eval()'d code 
> on line 1
> 
> If I just echo $contents, my browser spits out:
> 
> About the Project
> 
> 
> What's wrong? eval() is the correct thing to do, I thought?
> 

eval() expects PHP code, not HTML with embedded PHP code. Try this
(untested):

eval( " ?> $contents http://www.album.co.nz/
b: http://jbg.name/
p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303
a: PO Box 579, Christchurch 8015, New Zealand

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



[PHP] eval();

2005-10-30 Thread John Taylor-Johnston
I need to generate embedded php within in a mysql record. $contents is 
the actual contents of that record.


$contents = "arial,helvetica; font-weight: bold;\">About the Project

";
echo eval($contents);
?>

I get this error:
Parse error: parse error in /var/www/html2/test.php(4) : eval()'d code 
on line 1


If I just echo $contents, my browser spits out:

About the Project



What's wrong? eval() is the correct thing to do, I thought?

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



Re: [PHP] un-eval()

2005-10-30 Thread Chris Shiflett

John Taylor-Johnston wrote:

Does eval() have a synonym?


I don't usually like to answer a question with a question, but consider 
this:


eval($code);

This produces the following output:

red

Given this, what was the value of $code?

Chris

--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/

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



[PHP] un-eval()

2005-10-30 Thread John Taylor-Johnston

Does eval() have a synonym?

I store html in a mysql record, extract it into $contents. Under normal 
circumstances, I have to eval($contents) to get any embedded php code to 
work.


But when I use FCKeditor: 
http://testesp.flsh.usherb.ca/~johj2201/ramtest.php


$contents='';
$oFCKeditor->Value = $contents;
$oFCKeditor->Create() ;

Somehow FCKeditor eval()s $content. Under normal circumstances, I would 
ask the FCKeditor forum to address this. I would if I could point out to 
them the bug. But I cannot find any instances of eval().


Does eval() have a synonym? Or can I un-eval($contents)?

John

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



Re: [PHP] Using Ajax to spit out a php-generated embed tag

2005-10-30 Thread comex
> Can you use AJAX to output an entire php-generated
> embed tag into a web page?

You could use XMLHttpRequest's responseText property with innerHTML,
which is probably easier than using XML.

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



[PHP] Using Ajax to spit out a php-generated embed tag

2005-10-30 Thread Graham Anderson


Can you use AJAX to output an entire php-generated embed tag into a  
web page?


something  like:



height="352" width="216" codebase="http://www.apple.com/qtactivex/ 
qtplugin.cab">





EOB;

header('Content-Type: text/xml'); // ajax only seems to like text/xml
header('Content-Length: '.strlen($qtembed));
echo $qtembed;
?>



I am a bit new to Ajax stuff, but thankfully, have been able to make  
simple examples work.
From the examples I have tried thus far , it 'appears' that Ajax  
only supports sending/receiving elements like zip codenot entire  
tags
Is there a way to 'curl' the html to see what 'xmlhttprequest' is  
sending and receiving ?


Maybe I am going about this the wrong way and there is a better  
solution?
I wanted to avoid pop-up windows, launching new html pages,  and  
iframes if possible.
Ultimately, I want to dynamically write an invisible  movie embed tag  
into an html document when called by some 'geturl' actionscript.
Basically, click a button  in a flash movie, and  the quicktime  
player launches.


Instead, should I write a 'empty" quicktime movie tag into the html  
document with the element, moviename, for Ajax to target ?





many thanks in advance :)
g



this is the script I was attempting to modify:
http://developer.apple.com/internet/webcontent/XMLHttpRequestExample/ 
example.html




these are the two javascript functions where loadDoc is called by a  
form from the above html


function loadDoc(evt) {
// equalize W3C/IE event models to get event object
evt = (evt) ? evt : ((window.event) ? window.event : null);
if (evt) {
// equalize W3C/IE models to get event target reference
var elem = (evt.target) ? evt.target : ((evt.srcElement) ?  
evt.srcElement : null);

if (elem) {
try {
if (elem.selectedIndex > 0) {
loadXMLDoc(elem.options[elem.selectedIndex].value);
}
}
catch(e) {
var msg = (typeof e == "string") ? e :  
((e.message) ? e.message : "Unknown Error");

alert("Unable to get XML data:\n" + msg);
return;
}
}
}
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
var result = "";
if (prefix && isIE) {
// IE/Windows way of handling namespaces
result = parentElem.getElementsByTagName(prefix + ":" +  
local)[index];

} else {
// the namespace versions of this method
// (getElementsByTagNameNS()) operate
// differently in Safari and Mozilla, but both
// return value with just local name, provided
// there aren't conflicts with non-namespace element
// names
result = parentElem.getElementsByTagName(local)[index];
}
if (result) {
// get text, accounting for possible
// whitespace (carriage return) text nodes
if (result.childNodes.length > 1) {
return result.childNodes[1].nodeValue;
} else {
return result.firstChild.nodeValue;
}
} else {
return "n/a";
}
}

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



Re: [PHP] Re: Substr by words

2005-10-30 Thread Marcus Bointon


On 30 Oct 2005, at 15:35, Gustavo Narea wrote:

I think that trim($matches[0]) will return the whole string with no  
change.


No, it will return the entire matching pattern, not just the sub- 
matches. I added the trim to remove any leading space, and there will  
nearly always be a trailing space because of the part of my pattern  
that defines a word will include it. It was simpler to use trim than  
to make the pattern skip it. Did you actually try it?


On the other hand, I think we have to place a caret after the first  
slash.


Only if you insist that your string must start with a word - putting  
a ^ at the start would make it omit the first word if there was a  
space in front if it.



Instead of preg_match(), I had to type preg_replace():


err. I think you missed the point here. You don't need all that messy  
substr stuff at all. The preg_match already did it.


Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

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



[PHP] Re: Fabforce 4.0.5.6 Beta

2005-10-30 Thread Daniel Bowett

Vizion wrote:

I am trying to run Fabforce DBDesigner 4.0.5.6 Beta on:
win xp pro Version 2002 Service Pack 2
PHP 5.0.5
Mysql server 5.0.13 installed from 5.0.13-rc-win32.zip

and as Fabforce have closed their forum due to hacker problems I am hoping 
someopne here might have some idea how to solve a problem.


I get a 'dbexpress' username/password failure on any attempt by DBDesigner to 
connect to a database. PHPinfo reports presence of mysqli.


Does anyone have any ideas about  this problem?

david


I think you will need to set up a user account with an old style 
password in mysql. It worked for me.


http://dev.mysql.com/doc/refman/5.0/en/old-client.html

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



[PHP] Fabforce 4.0.5.6 Beta

2005-10-30 Thread Vizion
I am trying to run Fabforce DBDesigner 4.0.5.6 Beta on:
win xp pro Version 2002 Service Pack 2
PHP 5.0.5
Mysql server 5.0.13 installed from 5.0.13-rc-win32.zip

and as Fabforce have closed their forum due to hacker problems I am hoping 
someopne here might have some idea how to solve a problem.

I get a 'dbexpress' username/password failure on any attempt by DBDesigner to 
connect to a database. PHPinfo reports presence of mysqli.

Does anyone have any ideas about  this problem?

david
-- 
40 yrs navigating and computing in blue waters.
English Owner & Captain of British Registered 60' bluewater Ketch S/V Taurus.
 Currently in San Diego, CA. Sailing bound for Europe via Panama Canal after 
completing engineroom refit.

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



Re: [PHP] Re: Substr by words

2005-10-30 Thread Gustavo Narea

Other mistake in my last script.

Gustavo Narea wrote:

$MyOriginalString = "This is my original string.\nWhat do you think 
about this script?";

$MaxWords = 6; // How many words are needed?
$replacement = preg_match("/^(\W*\b\w+\b){1,$MaxWords}/", '', 
$MyOriginalString);
$result = substr( $MyOriginalString, 0, ($replacement) ? 
-strlen($replacement) : strlen($MyOriginalString));


echo $result;
?>


Instead of preg_match(), I had to type preg_replace():

$MyOriginalString = "This is my original string.\nWhat do you think 
about this script?";

$MaxWords = 6; // How many words are needed?
$replacement = preg_replace("/^(\W*\b\w+\b){1,$MaxWords}/", '', 
$MyOriginalString);
$result = substr( $MyOriginalString, 0, ($replacement) ? 
-strlen($replacement) : strlen($MyOriginalString));


echo $result;
?>

Best regards,

Gustavo Narea.

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



Re: [PHP] Re: Substr by words

2005-10-30 Thread Gustavo Narea

Hello.

Marcus Bointon wrote:

On 30 Oct 2005, at 06:22, Gustavo Narea wrote:
You could get the regex to do the search and the extraction in one go:

$MyOriginalString = "This is my original string.\nWhat do you think  
about this script?";

$MaxWords = 6; // How many words are needed?
$matches = array();
if (preg_match("/(\b\w+\b\W*){1,$MaxWords}/", $MyOriginalString,  
$matches)) {

$result = trim($matches[0]);
echo $result;
}


I have not used preg_* functions yet, so I may be wrong:

I think that trim($matches[0]) will return the whole string with no 
change. On the other hand, I think we have to place a caret after the 
first slash.


What about this:

$MyOriginalString = "This is my original string.\nWhat do you think 
about this script?";

$MaxWords = 6; // How many words are needed?
$matches = array();
if (preg_match("/^(\b\w+\b\W*){1,$MaxWords}/", $MyOriginalString, 
$matches)) {

unset($matches[0]);
$result = implode(" ",$matches);
echo $result;
}
?>

By the way, if you're able to use preg_* functions, I suggest you to use 
this script instead of the former I suggested. What's the difference?


Let's suppose we have a string with typos such as "Mandriva , Red Hat , 
Debian" (the right one is "Mandriva, Red Hat, Debian", without spaces 
before commas). The former script will find 6 words (because of the 
spaces before commas), while the latter will find 4 words (Mandriva Red 
Hat Debian). In this case, the former was wrong and the latter right.


However, the former doesn't not remove punctuation marks nor spaces 
(tabs, fine feeds, among others); the latter will remove any character 
which is a non-word character. If you need words + punctuation marks + 
spaces up to the ($MaxWords)th word, this is my suggestion:


$MyOriginalString = "This is my original string.\nWhat do you think 
about this script?";

$MaxWords = 6; // How many words are needed?
$replacement = preg_match("/^(\W*\b\w+\b){1,$MaxWords}/", '', 
$MyOriginalString);
$result = substr( $MyOriginalString, 0, ($replacement) ? 
-strlen($replacement) : strlen($MyOriginalString));


echo $result;
?>

Best regards,

Gustavo Narea.

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



Re: [PHP] php not activated

2005-10-30 Thread Marcus Bointon

On 30 Oct 2005, at 14:13, John Taylor-Johnston wrote:


echo $contents;


PHP doesn't now that it's PHP - it just treats it as text. You can  
tell it to run it as PHP explicitly using:


eval($contents);

Eval is usually worth avoiding, but it will do what you ask.

Your display function could be improved:

function display()
{
   $file = basename($_SERVER['PHP_SELF']);
   require 'connect.inc';
   $sql = "SELECT HTML FROM `$db`.`$table_editor` WHERE `Filename`  
LIKE '".addslashes($file)."' LIMIT 1;";

   if ($myquery = mysql_query($sql) and mysql_num_rows($myquery) > 0) {
   $mydata = mysql_fetch_array($myquery, MYSQL_NUM);
   return $mydata[0];
   }
   return false;
}

Then call it:

if ($contents = display())
eval($contents);

This should be faster and safer than your original code.

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

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



[PHP] php not activated

2005-10-30 Thread John Taylor-Johnston
I have some html + php stored in a mysql record. But when I echo the 
contents:


$mydata->HTML="";

the php is not activated; rather I see  in my html source. 
It worked before, I thought.


P.S. is there a better way to use this function?

John

snip-
display();
echo $contents;

function display()
{
   global $contents, $PHP_SELF;
   $file = basename($PHP_SELF);
   include("connect.inc");
   $sql = "SELECT * FROM `".$db."`.`".$table_editor."` WHERE `Filename` 
LIKE '".$file."' LIMIT 0,1;";

   $myquery = mysql_query($sql);
   while ($mydata = mysql_fetch_object($myquery)) {
   $contents = $mydata->HTML;
   }
}

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



Re: [PHP] Re: Substr by words

2005-10-30 Thread Marcus Bointon

On 30 Oct 2005, at 06:22, Gustavo Narea wrote:

$replacement = ereg_replace ("^([[:space:]]*[^[:space:][:cntrl:]]+) 
{1,$MaxWords}", "",$MyOriginalString);


echo substr( $MyOriginalString, 0, ($replacement) ? -strlen 
($replacement) : strlen($MyOriginalString));


You could get the regex to do the search and the extraction in one go:

$MyOriginalString = "This is my original string.\nWhat do you think  
about this script?";

$MaxWords = 6; // How many words are needed?
$matches = array();
if (preg_match("/(\b\w+\b\W*){1,$MaxWords}/", $MyOriginalString,  
$matches)) {

$result = trim($matches[0]);
echo $result;
}

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

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



Re: [PHP] [DONE] Substr by words

2005-10-30 Thread Marcus Bointon

On 29 Oct 2005, at 20:41, Richard Lynch wrote:


It was probably replacing *TWO* spaces with one.

If so, it should really be in a while loop, because there could be 3
or more spaces in a row, and if the goal is only single-spaced
words...


I can hardly think of a better application for a regex:

$text = preg_replace('/  */', ' ', $text);

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

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



Re: [PHP] Type of form element

2005-10-30 Thread Marcus Bointon

On 29 Oct 2005, at 20:59, Richard Lynch wrote:


So you will most likely be using isset($_POST['checkbox_name']) rather
than testing for "on"


I classify using isset for checking for the existence of array keys  
to be a bad habit as in some common cases it will not work as you  
expect, for example:




(note it has no value attribute) If it's checked, you would get the  
equivalent URL of "...?checkbox_name=", so $_REQUEST['checkbox_name']  
exists, but may contain NULL, and isset would return false even  
though it's there. A completely reliable check that will never  
generate any warnings is:


array_key_exists('checkbox_name', $_REQUEST).

If you have several checkboxes in an array (using names like  
name="checkbox_name[option1]"), you would say:


if (array_key_exists('checkbox_name', $_REQUEST) and is_array 
($_REQUEST['checkbox_name'])) {

if (array_key_exists('option1', $_REQUEST['checkbox_name'])) {
echo "you selected option 1\n";
}
if (array_key_exists('option2', $_REQUEST['checkbox_name'])) {
echo "you selected option 2\n";
}
//etc...
}

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

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