Sesquipedalian Posted December 29, 2007 Share Posted December 29, 2007 Hey everyone, I am using strpos() to find the position of a variable in a string, but I was wondering how i can find the position of a variable if its mentioned more than once, and one of the latter. ex. $string = 'There was a dog who was blue'; How can I find the position of the latter 'was', rather than the former one? Link to comment https://forums.phpfreaks.com/topic/83562-solved-finding-the-position-of-a-variable/ Share on other sites More sharing options...
sKunKbad Posted December 29, 2007 Share Posted December 29, 2007 <?php $string = 'There was a dog who was blue'; $word = 'was'; $text = explode($word,$string); $charnum = (strlen($text[0]) + strlen($word) + strlen($text[1]) + 1); echo $charnum; ?> Link to comment https://forums.phpfreaks.com/topic/83562-solved-finding-the-position-of-a-variable/#findComment-425139 Share on other sites More sharing options...
cooldude832 Posted December 29, 2007 Share Posted December 29, 2007 alternative method saves you an explode if you want the last one <?php $string = "There was a dog who was blue"; $needle = "was"; $needle = strrev($needle); $pos_las = strpos($string,$needle,strlen($needle)-1); echo "Position of last \"".strrev($needle)."\": ".$pos_las; ?> Should work Link to comment https://forums.phpfreaks.com/topic/83562-solved-finding-the-position-of-a-variable/#findComment-425141 Share on other sites More sharing options...
Sesquipedalian Posted December 29, 2007 Author Share Posted December 29, 2007 thanks! Link to comment https://forums.phpfreaks.com/topic/83562-solved-finding-the-position-of-a-variable/#findComment-425145 Share on other sites More sharing options...
sKunKbad Posted December 29, 2007 Share Posted December 29, 2007 There are lots of variations of how to derive the answer. I think this flexibility in php is what makes it so easy to learn. Take these other solutions for example: <?php echo "<br>"; $string = 'There was a dog who was blue'; $word = 'was'; $offset = strpos($string,$word); $secondPos = strpos($string,$word,$offset+1)+1; echo "The second position of the word \"$word\" in the string \"$string\" is at character number $secondPos."; ?> <?php echo "<br>"; $string = "There was a dog who was blue"; $revstring = strrev($string); $needle = "was"; $revneedle = strrev($needle); $pos_las = strlen($string) - strpos($revstring,$revneedle) - strlen($needle) +1; echo "The last position of \"". strrev($needle) ."\" in the string \"$string\" is at character number $pos_las"; ?> Link to comment https://forums.phpfreaks.com/topic/83562-solved-finding-the-position-of-a-variable/#findComment-425149 Share on other sites More sharing options...
Sesquipedalian Posted December 29, 2007 Author Share Posted December 29, 2007 That helps a lot, thank you. Link to comment https://forums.phpfreaks.com/topic/83562-solved-finding-the-position-of-a-variable/#findComment-425159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.