giventoflyfish Posted April 2, 2011 Share Posted April 2, 2011 I am trying to find the last word before a search pattern. I tried preg_match without any luck. I found something close online but it does a match for the first match for the pattern. I know that I will always see , some # pressure. I want that # right before the word pressure but there are a lot of , in the page. Thanks in advance. function get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $fullstring = "today, Sat, 750 pressure"; $parsed = get_string_between($fullstring, ", ", "pressure"); Quote Link to comment https://forums.phpfreaks.com/topic/232495-last-word-before-search-pattern/ Share on other sites More sharing options...
acefirefighter Posted April 2, 2011 Share Posted April 2, 2011 Can you post an example of the data you are trying to analyze, It would help greatly. Quote Link to comment https://forums.phpfreaks.com/topic/232495-last-word-before-search-pattern/#findComment-1195922 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 I think you want to use strrpos instead of strpos. It searches for the last occurrence in a string. Quote Link to comment https://forums.phpfreaks.com/topic/232495-last-word-before-search-pattern/#findComment-1196127 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.