【发布时间】:2014-02-18 22:38:54
【问题描述】:
我已经尝试了几个小时来获取由"{i}" 和"{[i]}" 分隔的字符串,例如{i}Esse{[i]},其中i 是一个整数。基于
get string between 2 strings。我不知道发生了什么,所以我决定寻求帮助。
我的代码是这样的:
<?php
include "keepGetInBetweenStrings.php";
$x['city1']='Esse';
$x['city2']='';
$x['city3']='é';
$x['city4']='um bom exemplo de';
$x['city5']=' uma portuguese-string!!';
$allCities='';
$cont=0;
for($i=1;$i<=5;$i++){
if($x['city'."$i"]!=''){
$cont=$cont+1;
$allCities=$allCities.'{'."$cont".'}'.$x['city'."$i"].'{['."$cont".']}';
}
}
echo $allCities;
echo "<br>";
for($i=1;$i<=5;$i++){
$token=getInbetweenStrings('{'."$i".'}', '{['."$i".']}', $allCities);
echo $token."<br>";
}
?>
<?php
function getInBetweenStrings($start, $end, $str){
echo $start."<br>";
echo $end."<br>";
$matches = array();
$regex = "/$start(.*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[0];
}
?>
非常感谢任何帮助。
输出是
{1}Esse{[1]}{2}é{[2]}{3}um bom exemplo de{[3]}{4} uma portuguese-string!!{[4]}
{1}
{[1]}
{2}
{[2]}
{3}
{[3]}
{4}
{[4]}
{5}
{[5]}
pHP 日志错误是
[2014 年 1 月 27 日星期一 19:08:20.406027] [:error] [pid 2638] [client 127.0.0.1:50728] PHP 警告:preg_match_all():编译失败:在 /var/ 中的偏移量 2 处没有可重复的内容第 8 行 www/NetBeansProjects/NewPhpProject/keepGetInBetweenStrings.php [Mon Jan 27 19:08:20.406039 2014] [:error] [pid 2638] [client 127.0.0.1:50728] PHP 注意:未定义的偏移量:/var/www/NetBeansProjects/NewPhpProject/keepGetInBetweenStrings.php 第 9 行中的 0
【问题讨论】: