ted_chou12 Posted April 12, 2007 Share Posted April 12, 2007 After using preg_split, I wish to find out the position of a word in a string of words, I wish to know if there is a preg function for that as well, please also give an example. Thanks, Ted Quote Link to comment Share on other sites More sharing options...
veridicus Posted April 12, 2007 Share Posted April 12, 2007 If you preg_split on whitespace the array position will tell you the word's position. I can't think of a way for preg to do it directly. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted April 12, 2007 Author Share Posted April 12, 2007 yes, i tried this: $pos = preg_split('/ /', $pquery1, -1, PREG_SPLIT_OFFSET_CAPTURE); but what it gives me is the position of each word in relate to the white spaces, however, it doesnt search a specific word, what it does is find out the starting position of each word in the string, and that is not very useful to me at all. Ted Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 12, 2007 Share Posted April 12, 2007 strpos() Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted April 12, 2007 Author Share Posted April 12, 2007 strpos limits the results, because I am also trying to match results that are case insensitive, so for Test, test tesT teST ... all of the combinations will match, however, not for strpos. Ted Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 12, 2007 Share Posted April 12, 2007 strpos limits the results, because I am also trying to match results that are case insensitive, so for So, before you run your comparison, just make a temp string in lowercase to test: <?php $word = strtolower($word); $temp = strtolower($string); $loc = strpos($temp, $word); ?> Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 12, 2007 Share Posted April 12, 2007 stripos() Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 12, 2007 Share Posted April 12, 2007 stripos() Great suggestion, but PHP5 only, so check your version before you try it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.