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

Posted by Marcus Bointon on 10/30/05 14:56

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
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk

 

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

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