0

我希望能够使用此字符串从数据库“{my-id-1}”中提取某些数据,因此基本上如果在文本“{my-id-*}”中找到该数据,则获取id(例如,如果 {is-id-1} 则 ID 为 1),然后我可以使用该 ID 运行一些代码。

所以我得到了它,所以我可以从大括号中获取 ID,但我不确定如何在文本中替换它。

<?php
$text = "test 1 dfhjsdh sdjkfhksdhfkj skjh {is-id-1} sdfhskdfh sdfsdjfhksd fjksdfhksd {is-id-2}";
preg_match_all('/{is-id-+(.*?)}/',$text, $matches);
print_r ($matches);

$replacewiththis = "this has been replaced, it was id: " . $idhere;
$text = preg_replace('/{is-id-+(.*?)}/', $replacewiththis, $text);

echo $text;
?>

匹配输出的数组:

Array ( 
  [0] => Array ( 
    [0] => {is-id-1} 
    [1] => {is-id-2} 
  ) 
  [1] => Array ( 
    [0] => 1 
    [1] => 2 
  ) 
)

我现在被困住了,不知道如何处理每个牙套。谁能帮我一把?

谢谢。

4

1 回答 1

1

我不确定我是否理解你想要什么,但我认为就是这样:

foreach($matches[1] as $match){
  $replacewiththis = "this has been replaced, it was id: $match";
  $text=str_replace('{is-id-'.$match.'}', $replacewiththis, $text);
}
echo $text;
于 2013-05-23T09:37:41.660 回答