Re: [PHP] [DONE] Substr by words

2005-10-31 Thread Marcus Bointon

On 31 Oct 2005, at 06:27, Richard Lynch wrote:


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.


I quite agree that many regexes are 'write only', but I don't think  
that that means that using substr and friends is necessarily any  
clearer. I sometimes find that a nested mass of string functions is  
even more confusing - at least a regex has a fixed grammar. I've just  
written a load of stuff that uses preg_replace_callback that I'm  
quite pleased with.


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



[PHP] [DONE] Substr by words

2005-10-29 Thread Danny
Finally i found it (Google is god, you only have to ask the right question)
 function trim_text($text, $count){
$text = str_replace( ,  , $text);
$string = explode( , $text);
for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ){
$trimed .= $string[$wordCounter];
if ( $wordCounter  $count ){ $trimed .=  ; }
else { $trimed .= ...; }
}
$trimed = trim($trimed);
return $trimed;
}

Usage

$string = one two three four;
echo trim_text($string, 3);


-- Forwarded message --
From: Danny [EMAIL PROTECTED]
Date: Oct 29, 2005 1:36 PM
Subject: Substr by words
To: php-general@lists.php.net

 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


--
dpc


Re: [PHP] [DONE] Substr by words

2005-10-29 Thread Minuk Choi

Good job!

However, let me give a few suggestions to optimize the code

function trim_text($text, $count)
{
$text = str_replace( ,  , $text);
/*
 * This is redundant; you are replacing all   in $text with  
	 *  maybe you meant 
	 *   $text = trim($text); ?
	 */ 


$string = explode( , $text);

	/* 
	 * For better programming practice, you should initialize $trimed

 *  I believe if you turn on error_reporting for all in php.ini
 *  PHP will display all warnings and errors.
 */


for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ )
	/* 
	 * Typo - you forgot the $ for wordCounter++

 *
	 * for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++) 
	 */

{
$trimed .= $string[$wordCounter];
if ( $wordCounter  $count )
{
			$trimed .=  ; 
		}
		else 
		{ 
			$trimed .= ...; 
		}

}

$trimed = trim($trimed);
return $trimed;
}



This is purely my suggestion... and I'm not saying it is better... but 
if I were you, I'd do it this way :


function trim_text($text, $count)
{
$text = trim($text);

$string = explode( , $text);
$trimed='';
for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++ )
{
$trimed .= $string[$wordCounter].' ';
}

$trimed = trim($trimed);

if (count($string)$count)
$trimed.='...';

return $trimed;
}

The only difference(not that you'd notice in a lightweight routine like 
this) is that, I don't have that if-else block inside the loop.



Danny wrote:


Finally i found it (Google is god, you only have to ask the right question)
function trim_text($text, $count){
$text = str_replace( ,  , $text);
$string = explode( , $text);
for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ){
$trimed .= $string[$wordCounter];
if ( $wordCounter  $count ){ $trimed .=  ; }
else { $trimed .= ...; }
}
$trimed = trim($trimed);
return $trimed;
}

Usage

$string = one two three four;
echo trim_text($string, 3);


-- Forwarded message --
From: Danny [EMAIL PROTECTED]
Date: Oct 29, 2005 1:36 PM
Subject: Substr by words
To: php-general@lists.php.net

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


--
dpc

 



--
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-29 Thread tg-php
Good start guys.  That's usually how I start down the path in solving a new 
problem.  Invariably down the road I find a more refined way of doing what I 
did the hard way.   The hard way is good though, you learn stuff along the way.

Let's see how tight we can make this though:

?php
$string =  This is a test string of some sort. ;
$truncated = truncstr($string);
echo $truncated;

function truncstr($tmpstr) {
  $maxwords = 4;

  $tmpstr = trim($tmpstr);
  $wordarr = explode( , $tmpstr);
  if (count($wordarr)  $maxwords) {
$wordarr = array_slice($wordarr, 0, $maxwords);
$tmpstr = implode( , $wordarr) . ...;
  } else {
$tmpstr = implode( , $wordarr);
  }
  return $tmpstr;
}
?

 Or if you want really obfuscated code...

?PHP
$string =  This is a test string of some sort. ;
$truncated = truncstr($string);
echo $truncated;

function truncstr($tmpstr) {
  return implode( , array_slice(explode( , trim($tmpstr)), 0, 4)) . ...;
}
?

-TG

= = = Original message = = =

Good job!

However, let me give a few suggestions to optimize the code

function trim_text($text, $count)

~$text = str_replace( ,  , $text);
~/*
~ * This is redundant; you are replacing all   in $text with  
~ *  maybe you meant 
~ *   $text = trim($text); ?
~ */ 

~$string = explode( , $text);
~
~/* 
 ~ * For better programming practice, you should initialize $trimed
 ~ *  I believe if you turn on error_reporting for all in php.ini
~ *  PHP will display all warnings and errors.
~ */


~for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ )
~/* 
~ * Typo - you forgot the $ for wordCounter++
~ *
~ * for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++) 
~ */
~
~~$trimed .= $string[$wordCounter];
~~if ( $wordCounter  $count )
~~
~~~$trimed .=  ; 
~~
~~else 
~~ 
~~~$trimed .= ...; 
~~
~

~$trimed = trim($trimed);
~return $trimed;




This is purely my suggestion... and I'm not saying it is better... but 
if I were you, I'd do it this way :

function trim_text($text, $count)

~$text = trim($text);

~$string = explode( , $text);
~$trimed='';
~for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++ )
~
~~$trimed .= $string[$wordCounter].' ';
~

~$trimed = trim($trimed);

~if (count($string)$count)
~~$trimed.='...';

~return $trimed;


The only difference(not that you'd notice in a lightweight routine like 
this) is that, I don't have that if-else block inside the loop.


Danny wrote:

Finally i found it (Google is god, you only have to ask the right question)
 function trim_text($text, $count)
$text = str_replace( ,  , $text);
$string = explode( , $text);
for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ )
$trimed .= $string[$wordCounter];
if ( $wordCounter  $count ) $trimed .=  ; 
else  $trimed .= ...; 

$trimed = trim($trimed);
return $trimed;


Usage

$string = one two three four;
echo trim_text($string, 3);


-- Forwarded message --
From: Danny [EMAIL PROTECTED]
Date: Oct 29, 2005 1:36 PM
Subject: Substr by words
To: php-general@lists.php.net

 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


--
dpc


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
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-29 Thread Richard Lynch
On Sat, October 29, 2005 10:51 am, Minuk Choi wrote:
 function trim_text($text, $count)
 {
   $text = str_replace( ,  , $text);
   /*
* This is redundant; you are replacing all   in $text with  
*  maybe you meant
*   $text = trim($text); ?
*/

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

//Replace 2 spaces with 1 until all words are single-spaced
while (strstr($text, '  ')) $text = str_replace('  ', ' ', $text);

PS

My post was doing 50 LETTERS, not 50 words.

So change 50 to 250 and call it done. :-)

Unless the word-count is really that critical...

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