note 74726 rejected from function.preg-match by nlopess
| From: | nlopess@php.net | Date: | Tue, 15 May 2007 13:49:11 +0000 |
| Subject: | note 74726 rejected from function.preg-match by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126612@lists.php.net to get a copy of this message | ||
Note Submitter: wallacehsh at gmail dot com
----
in PHP 5.2.X (above)
the preg_match is not work.
the sample code is:
<?php
$start_str = "<!--DOWNLOAD_PRINT_START_BLOCK-->";
$end_str = "<!--DOWNLOAD_PRINT_END_BLOCK-->";
$content_PHP = $start_str . "test" . $end_str;
$replace_script = "/(" . $start_str . ".*?" . $end_str . ")/";
preg_match($replace_script, $content_PHP, $result);
var_dump($result);
?>
in the older version like 5.1.6
it will be show the content.but PHP 5.2.x is not work.
so, in PHP 5.2.X, I try to do such thing in below:
-----------------------------------------------------------
<?php
if(!function_exists("returnSubstrings"))
{
/**
Returns an array containing each of the sub-strings from text that
are between openingMarker and closingMarker. The text from
openingMarker and closingMarker are not included in the result.
This function does not support nesting of markers.
*/
function returnSubstrings($text, $openingMarker, $closingMarker) {
$openingMarkerLength = strlen($openingMarker);
$closingMarkerLength = strlen($closingMarker);
$result = array();
$position = 0;
while (($position = strpos($text, $openingMarker, $position)) !== false) {
$position += $openingMarkerLength;
if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) {
$result[] = substr($text, $position, $closingMarkerPosition - $position);
$position = $closingMarkerPosition + $closingMarkerLength;
}
}
return $result;
}//end of function returnSubstrings($text, $openingMarker, $closingMarker)
}//end of if(!function_exists("returnSubstrings"))
$start_str = "<!--DOWNLOAD_PRINT_START_BLOCK-->";
$end_str = "<!--DOWNLOAD_PRINT_END_BLOCK-->";
$content_PHP = $start_str . "test" . $end_str;
$result = returnSubstrings($content_PHP,$start_str,$end_str);
var_dump($result);
?>
-----------------------------------------------------------