NYCPHP Meetup

NYPHP.org

[nycphp-talk] strpos with space

Jonathan hendler at simmons.edu
Mon Jul 25 16:37:35 EDT 2005


The offset doesn't work for me either the way it is documented here -- 
IF -- using spaces - but other characters work.
It doesn't say you can't use spaces, but I'd guess it only uses standard 
charactors as needles. I haven't tested \n's for example.

|// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0

|Is this a php version issue? I am using php 4.3.11.



Brian O'Connor wrote:

> The 250 offsets the 250th occurance of ' ', which therefore should be 
> right after the 250th word (assuming there aren't some random 
> occurances of it).
>
> http://us2.php.net/manual/en/function.strpos.php - note the last 
> example, where it returns 7 and not 1 (with the offset).
>
> >> Food for thought: what does your application do if it receives a 10,000
> >> character sequence of A's in a row without spaces?  Might it be that
> >> you want to break at 250 words OR a maximum number of characters?
>
> This function is only being used where administrators of the site 
> would be inputting data, so that right now isn't a concern for me.  
> However, once I get the site launched and start fine tuning my 
> scripts, I may also put a character limit on it with a certain ratio 
> of words to characters.
>
> On 7/25/05, *Jonathan* <hendler at simmons.edu 
> <mailto:hendler at simmons.edu>> wrote:
>
>     I am confused by the
>
>     "strpos($text, ' ', $words)"
>     because in this case $words represents 250 charactor offset for the
>     function strpos.
>     So instead of 250 words, wouldn't you get the first 250 charactors?
>
>     You want to find the position of the 250th word? I would use the index
>     250 of the array returned from str_word_count:
>
>     http://us3.php.net/manual/en/function.str-word-count.php
>     <http://us3.php.net/manual/en/function.str-word-count.php>
>     "2 - returns an associative array, where the key is the numeric
>     position
>     of the word inside the string and the value is the actual w"
>
>     Brian O'Connor wrote:
>
>     > I played around with those functions, and I think I can get them to
>     > work with what I need.
>     >
>     > However, I'm equally as interested in figuring out why this doesn't
>     > work as I am in getting a working function for what I need.
>     >
>     > Thanks for those functions btw.
>     >
>     > On 7/25/05, *Hans Zaunere* <lists at zaunere.com
>     <mailto:lists at zaunere.com>
>     > <mailto:lists at zaunere.com <mailto:lists at zaunere.com>>> wrote:
>     >
>     >
>     >     Here are some old functions I've used to do similar things.  I
>     >     can't guarantee how well they work, but they should give you
>     some
>     >     ideas at least.
>     >
>     >     http://www.pcomd.net/word_bevel <
>     http://www.pcomd.net/word_bevel>
>     >     http://www.pcomd.net/str_bevel <http://www.pcomd.net/str_bevel>
>     >
>     >
>     >     ---
>     >     Hans Zaunere
>     >     President, Founder
>     >     New York PHP
>     >     http://www.nyphp.org <http://www.nyphp.org/
>     <http://www.nyphp.org/>>
>     >
>     >     AMP Technology
>     >     Supporting Apache, MySQL and PHP
>     >
>     >
>     >        
>     ------------------------------------------------------------------------
>     >         *From:* talk-bounces at lists.nyphp.org
>     <mailto:talk-bounces at lists.nyphp.org>
>     >         <mailto:talk-bounces at lists.nyphp.org
>     <mailto:talk-bounces at lists.nyphp.org>>
>     >         [mailto:talk-bounces at lists.nyphp.org
>     <mailto:talk-bounces at lists.nyphp.org>
>     >         <mailto:talk-bounces at lists.nyphp.org
>     <mailto:talk-bounces at lists.nyphp.org>>] *On Behalf Of *Brian
>     >         O'Connor
>     >         *Sent:* Monday, July 25, 2005 2:42 PM
>     >         *To:* talk at lists.nyphp.org <mailto:talk at lists.nyphp.org>
>     <mailto:talk at lists.nyphp.org <mailto:talk at lists.nyphp.org>>
>     >         *Subject:* [nycphp-talk] strpos with space
>     >
>     >         I'm trying to create a function that displays the first 250
>     >         words of a text, and then puts " ... [ full text ] " (as a
>     >         link).  I did my research and I found the functions that I
>     >         thought would work, however when I try to findt he 250th
>     space
>     >         (to determine where the 250th word would be) it doesn't seem
>     >         to work, and the function only displays 250 characters.
>     >
>     >         Here is the function, maybe I am misunderstanding
>     something:
>     >
>     >         function createTextPreview($text, $words = 250)
>     >         {
>     >             if(str_word_count($text) < $words) {
>     >                 return $text;
>     >             } else {
>     >                 return substr($text, 0, strpos($text, ' ',
>     $words)) .
>     >         ' ... [ <a href="link">full text</a> ]' .
>     str_word_count($text);
>     >             }
>     >         }
>     >
>     >         Thanks in advance.
>     >
>     >         --
>     >         Brian O'Connor
>     >
>     >
>     >     _______________________________________________
>     >     New York PHP Talk Mailing List
>     >     AMP Technology
>     >     Supporting Apache, MySQL and PHP
>     >     http://lists.nyphp.org/mailman/listinfo/talk
>     <http://lists.nyphp.org/mailman/listinfo/talk>
>     >     http://www.nyphp.org
>     >
>     >
>     >
>     >
>     > --
>     > Brian O'Connor
>     >
>     >------------------------------------------------------------------------
>
>     >
>     >_______________________________________________
>     >New York PHP Talk Mailing List
>     >AMP Technology
>     >Supporting Apache, MySQL and PHP
>     > http://lists.nyphp.org/mailman/listinfo/talk
>     >http://www.nyphp.org
>     >
>
>     _______________________________________________
>     New York PHP Talk Mailing List
>     AMP Technology
>     Supporting Apache, MySQL and PHP
>     http://lists.nyphp.org/mailman/listinfo/talk
>     http://www.nyphp.org
>
>
>
>
> -- 
> Brian O'Connor
>
>------------------------------------------------------------------------
>
>_______________________________________________
>New York PHP Talk Mailing List
>AMP Technology
>Supporting Apache, MySQL and PHP
>http://lists.nyphp.org/mailman/listinfo/talk
>http://www.nyphp.org
>




More information about the talk mailing list