note 110667 deleted from ref.strings by aharvey
| From: | aharvey@php.net | Date: | Wed, 21 Nov 2012 01:51:46 +0000 |
| Subject: | note 110667 deleted from ref.strings by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192051@lists.php.net to get a copy of this message | ||
Note Submitter: holger1 at zentralplan dot com
----
This function returns an array of all fragments within $str between $start and $end.
Example: $image_tags = string_fragment($source, "<img ", ">", TRUE);
<?php
function string_fragment($str, $start, $end, $all) {
# $all == TRUE: $start and $end included; $all == FALSE: $start and $end excluded
$fragments = array(); $ce = "UTF-8";
while (mb_substr_count($str, $start, $ce)) {
$offset = mb_strpos($str, $start, 0, $ce);
if ($all) $len = 0; else $len = mb_strlen($start, $ce);
$rest = mb_substr($str, $offset + $len, mb_strlen($str, $ce), $ce);
if (mb_substr_count($rest, $end, $ce)) {
if ($all) $len = mb_strlen($end, $ce); else $len = 0;
$fragments[] = mb_substr($rest, 0, mb_strpos($rest, $end, 0, $ce) + $len, $ce);
}
$str = mb_substr($str, $offset + 1, mb_strlen($str, $ce), $ce);
}
return $fragments;
}
?>