你能告诉我如何用正则表达式替换吗

发布于 2024-09-12 00:39:01 字数 309 浏览 2 评论 0原文

你能否告诉如何用 preg-replace 替换字符串(需要正则表达式):

/user/{parent_id}/{action}/step/1

在数组的等效值处:

array('parent_id'=>32, 'action'=>'some');

制作:

/user/32/some/step/1

加法

这是一个典型的问题,所以我可能不知道变量的名称是什么

Could you tell how to replace string by preg-replace (need regular expression):

/user/{parent_id}/{action}/step/1

At the equivalent values of an array:

array('parent_id'=>32, 'action'=>'some');

To make:

/user/32/some/step/1

Addition

This is a typical problem, so I probably will not know what the names of variables come

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜中书 2024-09-19 00:39:01

您可以使用 str_replace

例如:

str_replace(array("{parent_id}", "{action}"), array(32, 'some'), "/user/{parent_id}/{action}/step/1");

You can use str_replace

For example:

str_replace(array("{parent_id}", "{action}"), array(32, 'some'), "/user/{parent_id}/{action}/step/1");
身边 2024-09-19 00:39:01
$arr = array('parent_id'=>32, 'action'=>'some');

$out = str_replace(array_keys($arr),array_values($arr),$in);

不需要正则表达式!

$arr = array('parent_id'=>32, 'action'=>'some');

$out = str_replace(array_keys($arr),array_values($arr),$in);

no need for regexps!

攀登最高峰 2024-09-19 00:39:01

假设您有:

$arr = array('parent_id'=>32, 'action'=>'some');
$in =  '/usr/{parent_id}/{action}/step/1';

这将替换大括号:

function bracelize($str) {
    return '{' . $str . '}';
}    

$search = array_map('bracelize', array_keys($arr));
$out = str_replace($search, $arr, $in);

或者如果您使用 PHP >= 5.3,则可以使用 lambda:

$search = array_map(function ($v) { return '{'.$v.'}';}, array_keys($arr));
$out = str_replace($search, $arr, $in);

Say you have:

$arr = array('parent_id'=>32, 'action'=>'some');
$in =  '/usr/{parent_id}/{action}/step/1';

This will replace the braces:

function bracelize($str) {
    return '{' . $str . '}';
}    

$search = array_map('bracelize', array_keys($arr));
$out = str_replace($search, $arr, $in);

Or if you are using PHP >= 5.3 you can use lambdas:

$search = array_map(function ($v) { return '{'.$v.'}';}, array_keys($arr));
$out = str_replace($search, $arr, $in);
泅人 2024-09-19 00:39:01
$s = '/user/{parent_id}/{action}/step/1';
$replacement = array('parent_id'=>32, 'action'=>'some');
$res = preg_replace(array('/\\{parent_id\\}/', '/\\{action\\}/'), $replacement, $s);

当然,您也可以使用str_replace(事实上,您应该)。

$s = '/user/{parent_id}/{action}/step/1';
$replacement = array('parent_id'=>32, 'action'=>'some');
$res = preg_replace(array('/\\{parent_id\\}/', '/\\{action\\}/'), $replacement, $s);

Of course, you could just as well use str_replace (in fact, you ought to).

不如归去 2024-09-19 00:39:01
<?php
  $string = '/usr/{parent_id}/{action}/step/1';
  $pattern = array('#{parent_id}#', '#{action}#');
  $values = array('32', 'some');

  echo preg_replace($pattern, $values, $string);
?>

如果您的问题不比这更复杂,我建议将 preg_replace 更改为 str_replace 。

编辑:我发现您事先不知道变量名称。在这种情况下你可以做这样的事情。

<?php
  function wrap_in_brackets(&$item)
  {
    $item = '{' . $item . '}';
    return true;
  }

  $string = '/usr/{parent_id}/{action}/step/1';
  $variables = array('parent_id' => 32, 'action' => 'some');

  $keys = array_keys($variables);
  array_walk($keys, 'wrap_in_brackets');
  echo str_replace($keys, array_values($variables), $string);
?>
<?php
  $string = '/usr/{parent_id}/{action}/step/1';
  $pattern = array('#{parent_id}#', '#{action}#');
  $values = array('32', 'some');

  echo preg_replace($pattern, $values, $string);
?>

If your problem is not more complicated than this, i would recommend changing preg_replace to str_replace though.

EDIT: I see you don't know the variable names in advance. In which case you could do something like this.

<?php
  function wrap_in_brackets(&$item)
  {
    $item = '{' . $item . '}';
    return true;
  }

  $string = '/usr/{parent_id}/{action}/step/1';
  $variables = array('parent_id' => 32, 'action' => 'some');

  $keys = array_keys($variables);
  array_walk($keys, 'wrap_in_brackets');
  echo str_replace($keys, array_values($variables), $string);
?>
岁月如刀 2024-09-19 00:39:01

扩展 mvds 的答案:

$in  = 'user/{parent_id}/{action}/step/1';
$arr = array('{parent_id}' => 32, '{action}' => 'some');
$out = str_replace(array_keys($arr), $arr, $in);

或者:

$in    = 'user/{parent_id}/{action}/step/1';
$arr   = array('parent_id' => 32, 'action' => 'some');
$arr[] = '';
$find  = array_merge(array_keys($arr), array('{', '}'));
$out   = str_replace($find, $arr, $in);

Expanding on mvds' answer:

$in  = 'user/{parent_id}/{action}/step/1';
$arr = array('{parent_id}' => 32, '{action}' => 'some');
$out = str_replace(array_keys($arr), $arr, $in);

Or:

$in    = 'user/{parent_id}/{action}/step/1';
$arr   = array('parent_id' => 32, 'action' => 'some');
$arr[] = '';
$find  = array_merge(array_keys($arr), array('{', '}'));
$out   = str_replace($find, $arr, $in);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文