preg_replace 自定义范围

发布于 2024-11-14 23:25:47 字数 283 浏览 0 评论 0原文

用户输入存储在变量 $input 中。

所以我想使用 preg Replace 来交换用户输入中从 az 开始的字母与我自己的自定义字母表。

我正在尝试的代码不起作用,如下所示:

preg_replace('/([a-z])/', "y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w", $input)

但是此代码不起作用。

如果有人对我如何使其正常工作有任何建议,那就太好了。谢谢

The user input is stored in the variable $input.

so i want to use preg replace to swap the letters from the user input that will range from a-z, with my own custom alphabet.

My code i am trying, which doesnt work is below:

preg_replace('/([a-z])/', "y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w", $input)

This code however doesnt work.

If anyone has any suggestions on how i can get this working then that would be great. Thanks

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

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

发布评论

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

评论(4

紧拥背影 2024-11-21 23:25:47

str 足够时,不要跳转到 preg

$regular = range('a', 'z');
$custom = explode(',', "y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w");
$output = str_replace($regular, $custom, $input);

Don't jump for preg, when str is enough:

$regular = range('a', 'z');
$custom = explode(',', "y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w");
$output = str_replace($regular, $custom, $input);
感悟人生的甜 2024-11-21 23:25:47

在这种情况下,使用 str_replace 更有意义:

str_replace(
    range("a", "z"), // Creates an array with all lowercase letters
    explode(",", "y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w"),
    $input
);

Using str_replace makes a lot more sense in this case:

str_replace(
    range("a", "z"), // Creates an array with all lowercase letters
    explode(",", "y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w"),
    $input
);
云醉月微眠 2024-11-21 23:25:47

您可以改为使用 strtr(),这解决了替换已替换值的问题。

echo strtr($input, 'abcdefghijklmnopqrstuvwxyz', 'ypltavkrezgmshubxncdijfqow');

使用 $input 作为 yahoo ,输出为 oyruu,正如预期的那样。

You could instead use strtr(), this resolves the problem of replacing already replaced values.

echo strtr($input, 'abcdefghijklmnopqrstuvwxyz', 'ypltavkrezgmshubxncdijfqow');

With $input as yahoo the output is oyruu, as expected.

以酷 2024-11-21 23:25:47

所给出的解决方案的一个潜在问题是每个字符可能发生多次替换。例如。 'a' 被 'y' 替换,并且在同一语句中 'y' 被 'o' 替换。因此,在上面给出的示例中,“aaa”变为“ooo”,而不是预期的“yyy”。 'yyy' 也变成 'ooo'。生成的字符串本质上是垃圾。如果有要求的话,你永远无法将其转换回来。

您可以使用两个替代品来解决这个问题。

在第一次替换时,您将 $regular 字符替换为 $input 中不存在的中间字符序列集。例如。 'a' 到 '[[[a]]]'、'b' 到 '[[[b]]]' 等。

然后将中间字符序列替换为您的 $custom 字符集。例如。 '[[[a]]]' 到 'y','[[[b]]]' 到 'p' 等。

就像这样...

$regular = range('a', 'z');
$custom = explode(',', 'y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w');

// Create an intermediate set of char (sequences) that don't exist anywhere else in the $input
// eg. '[[[a]]]', '[[[b]]]', ...
$intermediate = $regular;
array_walk($intermediate,create_function('&$value','$value="[[[$value]]]";'));

// Replace the $regular chars with the $intermediate set
$output = str_replace($regular, $intermediate, $input);

// Replace the $intermediate chars with our custom set
$output = str_replace($intermediate, $custom, $output);

编辑:

留下这个解决方案供参考,但 @salathe 使用 strtr() 的解决方案要好得多

A potential problem with the solutions given is that multiple replacements could occur for each character. eg. 'a' gets replaced by 'y', and in the same statement 'y' gets replaced by 'o'. So, in the examples given above, 'aaa' becomes 'ooo', not 'yyy' that might be expected. And 'yyy' becomes 'ooo' as well. The resulting string is essentially garbage. You'd never be able to convert it back, if that was a requirement.

You could get around this using two replacements.

On the first replacement you replace the $regular chars with an intermediate set of character sequences that don't exist in $input. eg. 'a' to '[[[a]]]', 'b' to '[[[b]]]', etc.

Then replace the intermediate character sequences with your $custom set of chars. eg. '[[[a]]]' to 'y', '[[[b]]]' to 'p', etc.

Like so...

$regular = range('a', 'z');
$custom = explode(',', 'y,p,l,t,a,v,k,r,e,z,g,m,s,h,u,b,x,n,c,d,i,j,f,q,o,w');

// Create an intermediate set of char (sequences) that don't exist anywhere else in the $input
// eg. '[[[a]]]', '[[[b]]]', ...
$intermediate = $regular;
array_walk($intermediate,create_function('&$value','$value="[[[$value]]]";'));

// Replace the $regular chars with the $intermediate set
$output = str_replace($regular, $intermediate, $input);

// Replace the $intermediate chars with our custom set
$output = str_replace($intermediate, $custom, $output);

EDIT:

Leaving this solution for reference, but @salathe's solution to use strtr() is much better!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文