You are here: Re: Substr by words « PHP « IT news, forums, messages
Re: Substr by words

Posted by Gustavo Narea on 10/30/05 08:22

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация