note 74726 added to function.preg-match
| From: | wallacehsh at gmail dot com | Date: | Wed, 25 Apr 2007 11:04:32 +0000 |
| Subject: | note 74726 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125852@lists.php.net to get a copy of this message | ||
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);
?>
-----------------------------------------------------------
----
Server IP: 140.112.2.5
Probable Submitter: 60.248.4.112 (proxied: 1.1 ISA)
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- https://master.php.net/note/edit/74726
Del: integrated -- https://master.php.net/note/delete/74726/integrated
Del: useless -- https://master.php.net/note/delete/74726/useless
Del: bad code -- https://master.php.net/note/delete/74726/bad+code
Del: spam -- https://master.php.net/note/delete/74726/spam
Del: non-english -- https://master.php.net/note/delete/74726/non-english
Del: in docs -- https://master.php.net/note/delete/74726/in+docs
Del: other reasons-- https://master.php.net/note/delete/74726
Reject -- https://master.php.net/note/reject/74726
Search -- https://master.php.net/manage/user-notes.php