PHP and javascript snippets you can copy and paste.

Saturday, March 17, 2007

str_get_pointsBetween($start,$end,$s,$n=0)

/** str_get_pointsBetween()
*
* Given start and end characters returns the start and end positions
* of the substring that includes the character at $n.
* @param string $start - the starting character.
* @param string $end - the end character.
* @param int $n - the position of the character that must be between $start and $end.
* @return array.
*/

function str_get_pointsBetween($start,$end,$s,$n=0){

$textUpToN = strrev(substr($s,0,$n+1));
$startCoord = strlen(substr($textUpToN,0,((strlen($textUpToN) - strpos($textUpToN,$start)) - strlen($textUpToN)==0?0:(strlen($textUpToN) - strpos($textUpToN,$start)))));
$endCoord = $n + (strpos((substr($s,$n)),$end));

return array($startCoord,$endCoord);

}

No comments: