I recently came across the need to efficiently parse a string and find what was in between two “marker” strings. I was parsing a logfile with a particular preset format. The line looked like this:
$str = '2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]';
I wanted to be able to easily pull out the ip and page information in a straight-forward way without repeating code. After a little tinkering I came up with a get_token() function that accomplished my goal nicely. The function is below:
function get_token($start_token, $end_token, $haystack, $offset = 0) { $start = stripos($haystack, $start_token, $offset); $end = stripos(substr($haystack, $start), $end_token); if ($start !== false && $end !== false) { $start_pos = $start + strlen($start_token); $end_pos = $end - strlen($end_token); return substr($haystack, $start_pos, $end_pos); } return false; }
Let’s take my above example – if I want to pull out the ip and page information from the given string, I only need to implement the below code:
$str = '2008-10-07_00:00:19 - ip:[213.21.198.231] page:[/phpsimplechat/documentation_usage.php]'; $ip = get_token('ip:[', ']', $str); $page = get_token('page:[', ']', $str);
The optional offset argument allows you to specify how many characters into the $haystack that you want to start looking;
Here is another example usage in which we want to pull the title out of some HTML code.
$str = 'This is the body'; $title = get_token('', $str);
