How can i get the word in a string when i know the position of the character ?

Ex:

$text = "Dit is een simpele text die nergens op slaat";
$positie = 18;

The function has to return the word: "simpele" because in this case the position is at 18. When you count back till a spatial, it returns the string which is found. This function counts back until it find a delimiter which is in this case a spatial.

// textstring is de text to search for
// positie is the position of the char
// delimiter is for ex a , or a spatial like: "," " "

function FindLastString($textstring, $positie, $delimiter)
{
?????
}

Someone ?

    Not sure that I understand you, or that what you are doing is the best way to go about this. How do you know that you need to start at 18? Will it alwasy be the last letter in the word? Makes a great deal of difference how one does this.

      Actually, "simpele" is at position 11.

      $text = "Dit is een simpele text die nergens op slaat"; 
      $positie = 11;
      $delimiter = ' ';
      echo FindLastString($text, $positie, $delimiter);
      
      function FindLastString($textstring, $positie, $delimiter) 
      { 
          if (!$end_pos = strpos($textstring, $delimiter, $positie + 1)) {
              return substr($textstring, $positie);
          } else {
              return substr($textstring, $positie, $end_pos - $positie);
          }
      }

        Yes Installer, but I think he wants a general solution to this. That is why I asked the questions.

          If starbbs knew in advance that it started at position 11 then he would not need to ask us how to find the starting position when all he knows is the position of one of the letters within the word.

          See his post.

            How can i get the word in a string when i know the position of the character ?

            I gave him what I understood him to ask for, and some code that can be easily adapted if it doesn't exactly answer it. Your questions are still there, and will hopefully help him.

              I think what is being asked for is "I have a string of words with spaces between them. What is the word that contains the 18th (or whatever) character?" The answer to that could be found with strpos() (using 18 as the offset parameter), strrpos() (using 18 as the offset parameter since PHP5) and substr().

              The question I have is "what if the 18th character is a space? Or punctuation?"

                I waqs not clear enough !

                The position is at 18, not 11. I got a functions that returns a LAST position. What i want is to count BACKWORDS until it finds a space. When i know this, i got the whole string.

                ex:

                $text = "just a test at work"
                $postion = "12

                the function returns: "test" because it ends after the t and than counts back till a space. That all chars between this space and the know position is returned.

                Anyone ?

                  Originally posted by Weedpacket
                  I think what is being asked for is "I have a string of words with spaces between them. What is the word that contains the 18th (or whatever) character?" The answer to that could be found with strpos() (using 18 as the offset parameter), strrpos() (using 18 as the offset parameter since PHP5) and substr().

                  The question I have is "what if the 18th character is a space? Or punctuation?"

                  This is what i meant. I want the word to return when i know the position. If the 18th char is a space or something else, it counts back until it finds a word

                    I really shouldnt be writing the code for you, but I wanted to give it a try (itchy fingers), so here's my take on it:

                    function parseForWord($str, $pos, $delim = ' ') {
                    	$len = strlen($str);
                    	//perform bounds checking
                    	if ($pos >= 0 && $pos < $len) {
                    		//is character at $pos is a delimiter?
                    		if ($str{$pos} != $delim) {
                    			//$pos marks a character within a word
                    			$word_len = 1;
                    			//scan left for start of word
                    			for ($start = $pos - 1; $start >= 0; $start--) {
                    				if ($str{$start} != $delim) {
                    					$word_len++;
                    				} else {
                    					break;
                    				}
                    			}
                    			//scan right to end of word
                    			for ($i = $pos + 1; $i < $len; $i++) {
                    				if ($str{$i} != $delim) {
                    					$word_len++;
                    				} else {
                    					break;
                    				}
                    			}
                    			//$start + 1 here because of $start-- or an out-of-bounds $start
                    			return substr($str, $start + 1, $word_len);
                    		} else {
                    			//$pos marks position of a delimiter
                    			$word_len = 0;
                    			//scan left only
                    			for ($i = $pos - 1; $i >= 0; $i--) {
                    				if ($str{$i} != $delim) {
                    					$word_len = 1;
                    					break;
                    				}
                    			}
                    			//$word_len = 1 > 0 if (last character of) word found
                    			if ($word_len > 0) {
                    				//scan left for start of word
                    				for ($start = $i - 1; $start >= 0; $start--) {
                    					if ($str{$start} != $delim) {
                    						$word_len++;
                    					} else {
                    						break;
                    					}
                    				}
                    				return substr($str, $start + 1, $word_len);
                    			} else {
                    				return '';
                    			}
                    		}
                    	} else {
                    		return false;
                    	}
                    }

                    Note that the position of a character within the string here is numbered from 0.

                      Whoooooaaaaa what a function.... i am really really impressed. I will try to study en learn from it ! is there really no simple solution to my request ? like a backwords strpos ?

                      I did find this... but sometimes it fails. Your function also returns a string if the position is in a middle of a string !! great !

                      Take a look at this one:

                      print array_pop(explode(' ',substr($text,0,$positie)));

                        Originally posted by starbbs
                        like a backwords strpos ?

                        [man]strpos[/man]
                        See also: [man]strrpos[/man]

                          is there really no simple solution to my request ?

                          Maybe, e.g. with regex, but I'm not sure how to go about constructing one that fits the bill.
                          The complexity lies in the way you define words, and deal with them when the position is actually a delimiter's position.

                          like a backwords strpos ?

                          I think you mean the reverse function of strpos() rather than reversed strpos().

                            Well laserlight.... i am really greatfull for your help. I did not realise that this request isn´t all that simple.

                            But did you take a look at my example ±
                            print array_pop(explode(' ',substr($text,0,$positie)));

                              print array_pop(explode(' ',substr($text,0,$positie)));

                              At first glance, this might be ok, though you'll have problems with multiple delimiters.

                              The code example I gave you was originally coded by defining words as alphanumeric strings, so ctype_alnum() was used to test instead of a direct comparison with the delimiter.
                              This allows you to work with more than one delimiter with just a small modification of the function.

                                Obviously, the real code is the content of the second loop. The loops are only there as test harnesses to make sure that the right words are returned for every possible value of $pos.

                                $sentence = "Dit is een simpele text die nergens op slaat. I'd write this sentence in Dutch if I knew any Dutch."; 
                                
                                // Okay, let's test this.
                                for($pos = 0; $pos<strlen($sentence); ++$pos)
                                	echo $pos,' ',array_pop(explode(' ',substr($sentence,0,$pos))),"\n";
                                
                                // Not what is desired, I take on.
                                
                                // Regexps? Okay, let's have a crack with them.
                                
                                for($pos=0; $pos<strlen($sentence); ++$pos)
                                {
                                	// Break the sentence into to pieces at $pos. The word we want will be the 
                                	// last one on the left. If $pos was inside the word at the time, a fragment
                                	// of the word will end up on the right.
                                	$left = substr($sentence, 0, $pos);
                                	$right = substr($sentence, $pos);
                                	// Isolate the last word to the left of $pos and the first word to the right of $pos.
                                	// There may be some whitespace after the last word, and there may be some before the first.
                                	preg_match('/(\\S*)(\\s*)$/', $left, $last_word_and_space);
                                	preg_match('/^(\\s*)(\\S*)/', $right, $space_and_first_word);
                                	// We don't need $junk - it's the entire string matched - word and space and all.
                                	list($junk, $last_word, $space_on_left) = $last_word_and_space;
                                	list($junk, $space_on_right, $first_word) = $space_and_first_word;
                                	if($space_on_left=='' && $space_on_right=='')
                                	{
                                		// $pos lay inside a word, which got bro
                                		// ken into two pieces.
                                		$word = $last_word.$first_word;
                                	}
                                	elseif($space_on_right!='')
                                	{
                                		// $pos was in whitespace
                                		//  at the end of a word
                                		$word = $last_word;
                                	}
                                	elseif($space_on_left!='') // Don't really need to test this
                                	{
                                		// $pos was positioned 
                                		// at the start of the word
                                		$word = $first_word;
                                	}
                                	echo $pos,' ',$word,"\n";
                                }
                                

                                (Edit: just noticed vBulletin had turned my \s's and \S's into s's and S's. That's not right.)

                                  Thanks for this one... but i really have to make this last code work cause it does not returns the found word left at the position i know/want ?

                                  SO what does this one ?

                                    function parseForWord($str, $pos, $delim = ' ')

                                    I also saw that this one also return words like:

                                    name,

                                    You see the , at the end ? i thoight it tests this to filter this out ?