繁体   English   中英

用字符串中的匹配记录替换给定字符串的所有出现

[英]Replace all occurrences of a given string with matching records in a string

我的问题是:我有一个字符串。 在此字符串中,有两个 % 字符内的 ID。 对于每个 ID,在多维数组中都有匹配的记录。 数组键是两个 % 字符之间的 ID。 对于字符串中的每次出现,我想用相应的记录替换 ID 和 2% 字符。

字符串链示例:

Lorem %1% dolor sit amet, consetetur sadipscing elitr, sed diam %5% eirmod tempor invidunt ut labore et dolore %7% aliquyam erat, sed diam voluptua.

包含用户数据的数组示例:

Array ( [0] => Array ( [id] => 0 [data] => LOREM ) [1] => Array ( [id] => 1 [data] => IPSUM ) ) 

我已经尝试使用 explode 将整个字符串链转换为一个数组,然后使用 foreach 循环遍历数组条目。 不幸的是,字符串中的空格给我带来了问题,所以explode不能正常工作,我无法在自己的数组条目中获取所有单词。

到目前为止,我有一个 function“getContents()”,我可以用它从整个字符串链中获取所有出现的 2% 字符和 ID。 我使用 ID 从数据库中提取相应的记录并将它们存储在一个数组中。

  function getContents($str, $startDelimiter = '%', $endDelimiter = '%') {

    $contents = array();
    $startDelimiterLength = strlen($startDelimiter);
    $endDelimiterLength = strlen($endDelimiter);
    $startFrom = $contentStart = $contentEnd = 0;

    while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {

      $contentStart += $startDelimiterLength;
      $contentEnd = strpos($str, $endDelimiter, $contentStart);

      if (false === $contentEnd) {
        break;
      }

      $contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
      $startFrom = $contentEnd + $endDelimiterLength;

    }

    return $contents;

  }

  function getUserDataFields($input, $userid)
  {

    $dataFields = $this->getContents($input);

    foreach($dataFields as $key => $field){
      $datas[$key] = ["id" => $key, "data" => $this->userController->getCustomerDataByFieldId($userid, $field)];
    }


//At this point I want to loop through the whole string chain in some way to replace All Ids with the corresponding separators with the matching records and then return the whole string from the function.

  }

在这里你可以传入一个数组、字符串和分隔符包装器,如果它在数组中找到它,那么它将替换它; 否则别管它。

$str = 'Lorem %1% dolor sit amet, consetetur sadipscing elitr, sed diam %0% eirmod tempor invidunt ut labore et dolore %7% aliquyam erat, sed diam voluptua.';

$arr = [
  [
    'id' => 0,
    'data' => 'LOREM'
  ],
  [
    'id' => 1,
    'data' => 'IPSUM'
  ]
];

$newStr = replaceContent($arr, $str);

echo $newStr;

function replaceContent($arr, $str, $startDelimiter = '%', $endDelimiter = '%') {
  preg_match_all("#{$startDelimiter}([\d]+){$endDelimiter}#", $str, $matches);

  foreach($matches[1] as $key => $value) {
    $index = array_search($value, array_column($arr, 'id'));
    if ($index === false) continue;

    $str = str_replace($matches[0][$key], $arr[$index]['data'], $str);
  }

  return $str;
}

您可以提取由id索引的数组,然后使用找到的数字替换:

$array = array_column($array, 'data', 'id');
$text = preg_replace_callback('/%(\d+)%/', function ($m) use($array) {
                                               return $array[$m[1]];
                                           }, $text);

或者只是一个使用这些索引的简单循环:

foreach(array_column($array, 'data', 'id') as $key => $val) {
    $text = str_replace("%$key%", $val, $text);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM