Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP preg用循环替换问题_Php_Regex_Preg Replace - Fatal编程技术网

PHP preg用循环替换问题

PHP preg用循环替换问题,php,regex,preg-replace,Php,Regex,Preg Replace,我需要以下代码的一些帮助。我在一个表上有一个字段,需要在其中替换各种标记,并且我已经确定了针对特定区域所需的正则表达式模式。这段代码将这些内容提取出来,并将它们剥离成我想要的格式,但是。。。它在主体中为它们分配相同的“imageId”——数组中的最后一个。我知道它没有以正确的方式循环,只是需要另一组眼睛。我感觉如果它通过身体并替换一个标签一次而不是在for循环的中间,它可以做它应该做的。当前字段主体如下所示: <text:image id="12345" blah blah blah /&

我需要以下代码的一些帮助。我在一个表上有一个字段,需要在其中替换各种标记,并且我已经确定了针对特定区域所需的正则表达式模式。这段代码将这些内容提取出来,并将它们剥离成我想要的格式,但是。。。它在主体中为它们分配相同的“imageId”——数组中的最后一个。我知道它没有以正确的方式循环,只是需要另一组眼睛。我感觉如果它通过身体并替换一个标签一次而不是在for循环的中间,它可以做它应该做的。当前字段主体如下所示:

<text:image id="12345" blah blah blah />

lots of text here

<text:image id="123456" blah blah blah />

lots of text here

<text:image id="7890" blah blah blah />
<text:image id="12345"/>

lots of text here

<text:image id="123456" />

lots of text here

<text:image id="7890" />

有几种方法可以改进这一点。我手头没有我的正则表达式测试仪,但有点像

while($row2 = $textToReplace2->fetch_array()) {
  $t1_id = $row2["id"];
  $body = $row2["body"];
  $replace='$1>'; // some experimentation needed to find correct index
  $new=preg_replace('#(<text\:image id="(\d+)")([^>]+)>#U', $replace, $body);
  $newEscapedBody = mysqli_real_escape_string($conn, $new);
while($row2=$textToReplace2->fetch_array()){
$t1_id=$row2[“id”];
$body=$row2[“body”];
$replace='$1>';//需要一些实验来找到正确的索引
$new=preg#U replace('#(]+)>#U',$replace,$body);
$newEscapedBody=mysqli\u real\u escape\u字符串($conn,$new);

所有这些括号在哪里关闭?复制粘贴问题,它们在那里
更新T1 SET body='$newEscapedBody'其中id='$T1_id'
在循环的每个步骤中,您都在用id
$T1_id
更新记录。在每个步骤中,您都在一次又一次地更新同一条记录。因此,您正在更新的值循环的最后一步留在表中。是的,但即使删除该更新,转储newEscapedBody也会显示每次要设置的第一个文本id…您能详细说明一下您采用的索引方法吗?只是尝试学习阅读pcre,尤其是子表达式
while($row2 = $textToReplace2->fetch_array()) {
  $t1_id = $row2["id"];
  $body = $row2["body"];
  $replace='$1>'; // some experimentation needed to find correct index
  $new=preg_replace('#(<text\:image id="(\d+)")([^>]+)>#U', $replace, $body);
  $newEscapedBody = mysqli_real_escape_string($conn, $new);