php-general Digest 30 Oct 2005 15:36:19 -0000 Issue 3766

Topics (messages 224837 through 224845):

Re: Substr by words
        224837 by: Gustavo Narea
        224838 by: Gustavo Narea
        224839 by: Gustavo Narea
        224842 by: Marcus Bointon
        224845 by: Gustavo Narea

Re: Type of form element
        224840 by: Marcus Bointon

Re: [DONE] Substr by words
        224841 by: Marcus Bointon

php not activated
        224843 by: John Taylor-Johnston
        224844 by: Marcus Bointon

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:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message --- If forgot to say that It counts ($MaxWords) words, It doesn't matter if they're separated by simple spaces, line feeds (Unix, dos or mac), tabs, among others.

On the other hand, you won't have any problem if you use non-English characters.

Best regards,

Gustavo Narea.

Gustavo Narea wrote:
Hello.

What do you think about this:

<?php

$MyOriginalString = "This is my original string.\nWhat do you think about this script?";
$MaxWords = 6; // How many words are needed?

echo substr( $MyOriginalString, 0, -strlen(ereg_replace ("^([[:space:]]*[^[:space:][:cntrl:]]+){1,$MaxWords}", "",$MyOriginalString)));
?>

Only 3 lines.

You have to change $MaxWords to 50 if that's what you need.

Best regards,

Gustavo Narea.

Danny wrote:

Hi,
I need to extract 50 words more or less from a description field. How can i
do that?. Substr, cuts the words. Is there any other way to that, without
using and array? I mean and implemented function in PHP 4.x
 I´ve been googling around, but wordwrap, and substr is driving me mad...
 Thanks in advance
Best Regards

--
dpc


--- End Message ---
--- Begin Message ---
Gustavo Narea wrote:
If forgot to say that It counts ($MaxWords) words, It doesn't matter if they're separated by simple spaces, line feeds (Unix, dos or mac), tabs,
And punctuation marks.

Sorry, I'm very forgetful tonight!

Cheers.

--- End Message ---
--- Begin Message --- My script will fail if the amount of words in $MyOriginalString is less than $MaxWords. So, this is the new suggestion:

<?php

$MyOriginalString = "This is my original string.\nWhat do you think about this script?";
$MaxWords = 50; // How many words are needed?
$replacement = ereg_replace ("^([[:space:]]*[^[:space:][:cntrl:]]+){1,$MaxWords}", "",$MyOriginalString);

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

Four lines.

BTW: Did I mention that I was forgetful? ;-)

Best regards,

Gustavo Narea.

Gustavo Narea wrote:
Hello.

What do you think about this:

<?php

$MyOriginalString = "This is my original string.\nWhat do you think about this script?";
$MaxWords = 6; // How many words are needed?

echo substr( $MyOriginalString, 0, -strlen(ereg_replace ("^([[:space:]]*[^[:space:][:cntrl:]]+){1,$MaxWords}", "",$MyOriginalString)));
?>

Only 3 lines.

You have to change $MaxWords to 50 if that's what you need.

Best regards,

Gustavo Narea.

Danny wrote:

Hi,
I need to extract 50 words more or less from a description field. How can i
do that?. Substr, cuts the words. Is there any other way to that, without
using and array? I mean and implemented function in PHP 4.x
 I´ve been googling around, but wordwrap, and substr is driving me mad...
 Thanks in advance
Best Regards

--
dpc


--- End Message ---
--- Begin Message ---
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

--- End Message ---
--- Begin Message ---
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:

<?php
$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:

<?php
$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.

--- End Message ---
--- Begin Message ---
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:

<input type="checkbox" name="checkbox_name" />

(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

--- End Message ---
--- Begin Message ---
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

--- End Message ---
--- Begin Message --- I have some html + php stored in a mysql record. But when I echo the contents:

$mydata->HTML="<input type="text" size="40" value="<?php
   if($searchenquiry)
   echo stripslashes(htmlspecialchars($searchenquiry));
   ?>" name="searchenquiry" />";

the php is not activated; rather I see <?php ... ?> 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;
   }
}

--- End Message ---
--- Begin Message ---
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

--- End Message ---

Reply via email to