Re: [PHP] Divide into words
how about ... Nope. The question was the position of the words, not how many times they appear. oops :( -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Divide into words
> > For example i have some words: > > > > Today is very beautiful day and sun is shining > > > > What i want to get from this is array > > > > words > > [Today] => 0 > > [Is] => 6,30 > > [Very] => 8 > > [beautiful] => 12 > > .. > > > > Can somebody please help me with this. Those nubers are > > position of special word in above sentence. If word repeates > > i want to have both positions saved in same row like word is. > > > > I tried something but i think it's to lame. > > > > > > how about ... > > > $sentance = "Today is a beautiful day. "; > $temp = preg_replace("/[^a-zA-Z]+/", " ", $sentance); > $temp = explode(' ', $temp); > $words = array_count_values($temp); > > print_r($words); Nope. The question was the position of the words, not how many times they appear. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Divide into words
Uros Gruber wrote: Hi! For example i have some words: Today is very beautiful day and sun is shining What i want to get from this is array words [Today] => 0 [Is] => 6,30 [Very] => 8 [beautiful] => 12 .. Can somebody please help me with this. Those nubers are position of special word in above sentence. If word repeates i want to have both positions saved in same row like word is. I tried something but i think it's to lame. how about ... -- Sean -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Divide into words
> Yes, but my example was geared towards trying to work through a more > generic scenario. I'll bet if you try using perl regex with callback > it'll be even faster. I'll take that bet and say using preg_*_callback will be slower. :) I'll try and time each of them tonite and post what I find. Something else to distract me from writing my column! ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Divide into words
Yes, but my example was geared towards trying to work through a more generic scenario. I'll bet if you try using perl regex with callback it'll be even faster. Marco -- php|architect - The magazine for PHP Professionals The monthly worldwide magazine dedicated to PHP programmers Come visit us at http://www.phparch.com! --- Begin Message --- Hi! I have dome almost the same $input = 'Today is a very beautiful day and the sun is shining'; $output = array(); $words = explode(' ',$input); $cur_pos = 0; foreach($words as $word) { if(!empty($output[$word])){ $output[$word] .= ','.$cur_pos; } else { $output[$word] = $cur_pos; } $cur_pos += strlen($word) + 1; } unset ($word); but i thought if there is some better way. I also tried Marco's example and adjust it a little bit to get the same results. And here is some benchmark i have. with this example i get 0.0024310350418091 and with Marco's i get 0.0034389495849609s So using two array is faster anyway. Probably of explode speed. -- bye, Uros Monday, December 16, 2002, 6:36:37 PM, you wrote: >> For example i have some words: >> >> Today is very beautiful day and sun is shining >> >> What i want to get from this is array >> >> words >> [Today] => 0 >> [Is] => 6,30 >> [Very] => 8 >> [beautiful] => 12 >> .. >> >> Can somebody please help me with this. Those nubers are >> position of special word in above sentence. If word repeates >> i want to have both positions saved in same row like word is. >> >> I tried something but i think it's to lame. JWH> Well you could've at least shown us what you had so far instead of JWH> letting us solve the problem for you: JWH> $string = "Today is a very beautiful day and the sun is shining"; JWH> $words = explode(" ",$string); JWH> $pos = 0; JWH> foreach($words as $word) JWH> { JWH> if(isset($w[$word])) JWH> { $w[$word] .= ",$pos"; } JWH> else JWH> { $w[$word] = $pos; } JWH> $pos += strlen($word) + 1; JWH> } JWH> print_r($w); JWH> ---John W. Holmes... JWH> PHP Architect - A monthly magazine for PHP Professionals. Get your copy JWH> today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- End Message --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Divide into words
Hi! I have dome almost the same $input = 'Today is a very beautiful day and the sun is shining'; $output = array(); $words = explode(' ',$input); $cur_pos = 0; foreach($words as $word) { if(!empty($output[$word])){ $output[$word] .= ','.$cur_pos; } else { $output[$word] = $cur_pos; } $cur_pos += strlen($word) + 1; } unset ($word); but i thought if there is some better way. I also tried Marco's example and adjust it a little bit to get the same results. And here is some benchmark i have. with this example i get 0.0024310350418091 and with Marco's i get 0.0034389495849609s So using two array is faster anyway. Probably of explode speed. -- bye, Uros Monday, December 16, 2002, 6:36:37 PM, you wrote: >> For example i have some words: >> >> Today is very beautiful day and sun is shining >> >> What i want to get from this is array >> >> words >> [Today] => 0 >> [Is] => 6,30 >> [Very] => 8 >> [beautiful] => 12 >> .. >> >> Can somebody please help me with this. Those nubers are >> position of special word in above sentence. If word repeates >> i want to have both positions saved in same row like word is. >> >> I tried something but i think it's to lame. JWH> Well you could've at least shown us what you had so far instead of JWH> letting us solve the problem for you: JWH> $string = "Today is a very beautiful day and the sun is shining"; JWH> $words = explode(" ",$string); JWH> $pos = 0; JWH> foreach($words as $word) JWH> { JWH> if(isset($w[$word])) JWH> { $w[$word] .= ",$pos"; } JWH> else JWH> { $w[$word] = $pos; } JWH> $pos += strlen($word) + 1; JWH> } JWH> print_r($w); JWH> ---John W. Holmes... JWH> PHP Architect - A monthly magazine for PHP Professionals. Get your copy JWH> today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Divide into words
True, but it answers the original question. :) You can easily throw in a if($word == ' ') then don't save it, but still increment $pos. You could use a regular expression to check/remove punctuation, too. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ > -Original Message- > From: Marco Tabini [mailto:[EMAIL PROTECTED]] > Sent: Monday, December 16, 2002 12:37 PM > To: [EMAIL PROTECTED] > Cc: 'Uros Gruber'; PHP-General > Subject: RE: [PHP] Divide into words > > John, > > I'm not sure this will work if there is more than one space between > words. > > Also, my previous example won't work if there are duplicate words > (although it's easy to make an array out of each word and solve the > problem), nor if the words are delimited by any character other than > spaces, in which case regex are probably our best bet. > > Cheers, > > > Marco > -- > > php|architect - The magazine for PHP Professionals > The monthly worldwide magazine dedicated to PHP programmers > > Come visit us at http://www.phparch.com! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Divide into words
John, I'm not sure this will work if there is more than one space between words. Also, my previous example won't work if there are duplicate words (although it's easy to make an array out of each word and solve the problem), nor if the words are delimited by any character other than spaces, in which case regex are probably our best bet. Cheers, Marco -- php|architect - The magazine for PHP Professionals The monthly worldwide magazine dedicated to PHP programmers Come visit us at http://www.phparch.com! --- Begin Message --- > For example i have some words: > > Today is very beautiful day and sun is shining > > What i want to get from this is array > > words > [Today] => 0 > [Is] => 6,30 > [Very] => 8 > [beautiful] => 12 > .. > > Can somebody please help me with this. Those nubers are > position of special word in above sentence. If word repeates > i want to have both positions saved in same row like word is. > > I tried something but i think it's to lame. Well you could've at least shown us what you had so far instead of letting us solve the problem for you: $string = "Today is a very beautiful day and the sun is shining"; $words = explode(" ",$string); $pos = 0; foreach($words as $word) { if(isset($w[$word])) { $w[$word] .= ",$pos"; } else { $w[$word] = $pos; } $pos += strlen($word) + 1; } print_r($w); ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- End Message --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Divide into words
> For example i have some words: > > Today is very beautiful day and sun is shining > > What i want to get from this is array > > words > [Today] => 0 > [Is] => 6,30 > [Very] => 8 > [beautiful] => 12 > .. > > Can somebody please help me with this. Those nubers are > position of special word in above sentence. If word repeates > i want to have both positions saved in same row like word is. > > I tried something but i think it's to lame. Well you could've at least shown us what you had so far instead of letting us solve the problem for you: $string = "Today is a very beautiful day and the sun is shining"; $words = explode(" ",$string); $pos = 0; foreach($words as $word) { if(isset($w[$word])) { $w[$word] .= ",$pos"; } else { $w[$word] = $pos; } $pos += strlen($word) + 1; } print_r($w); ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Divide into words
You could use strpos--there are easier ways if you don't need the offsets. Example: This is from memory, but it looks like it should work even for weird strings like the one above. Cheers, Marco -- php|architect - The magazine for PHP Professionals The monthly worldwide magazine dedicated to PHP programmers Come visit us at http://www.phparch.com! --- Begin Message --- Hi! For example i have some words: Today is very beautiful day and sun is shining What i want to get from this is array words [Today] => 0 [Is] => 6,30 [Very] => 8 [beautiful] => 12 .. Can somebody please help me with this. Those nubers are position of special word in above sentence. If word repeates i want to have both positions saved in same row like word is. I tried something but i think it's to lame. -- bye, Uros -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- End Message --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Divide into words
Hi! For example i have some words: Today is very beautiful day and sun is shining What i want to get from this is array words [Today] => 0 [Is] => 6,30 [Very] => 8 [beautiful] => 12 .. Can somebody please help me with this. Those nubers are position of special word in above sentence. If word repeates i want to have both positions saved in same row like word is. I tried something but i think it's to lame. -- bye, Uros -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php