You are here

function table_altrow_str_replace_count in Table Alternate Rows 7

Same name and namespace in other branches
  1. 8 table_altrow.module \table_altrow_str_replace_count()

Replace every instance of a string with a count parameter like PHP5. This can probably be removed with Drupal goes to PHP5 only. Shamelessly stolen and modified from http://ca.php.net/manual/en/function.str-replace.php#76180

Parameters

$needle: The string to search for.

$replace: The string to replace.

$haystack: The text to search within.

$offset: Optional parameter to indicate the character position to start the search and replace at.

integer $count: Optional parameter to indicate the number of times to execute replacements.

Return value

The modified string.

1 call to table_altrow_str_replace_count()
_table_altrow_process in ./table_altrow.module
Filter process callback.

File

./table_altrow.module, line 100
Insert even and odd classes for tables via input filters to allow for proper zebra-style striping.

Code

function table_altrow_str_replace_count($needle, $replace, $haystack, $offset = NULL, $count = NULL) {
  if ($count == null) {
    $count = 0;
    $offset = strpos($haystack, $needle);
  }
  $rpl_count = 0;
  while ($offset !== false && $rpl_count < $count) {
    $haystack = substr_replace($haystack, $replace, $offset, strlen($needle));
    $offset += strlen($replace);
    $offset = strpos($haystack, $needle, $offset);
    $rpl_count++;
  }
  return $haystack;
}