如何将一个字符串分成两部分然后以相反的顺序将它们连接为一个新字符串? [英] How to split a string into two parts then join them in reverse order as a new string?

查看:25
本文介绍了如何将一个字符串分成两部分然后以相反的顺序将它们连接为一个新字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子:

$str="this is string 1 / 4w";
$str=preg_replace(?); var_dump($str);

我想在这个字符串中捕获 1/4w 并将这部分移动到字符串的开头.

I want to capture 1 / 4w in this string and move this portion to the begin of string.

结果:1/4W 这是字符串

只需给我包含捕获的变量即可.

Just give me the variable that contains the capture.

最后一部分 1/4W 可能不同.

The last portion 1 / 4W may be different.

例如1/4w 可以是 1/16W1/2W1W2w

e.g. 1 / 4w can be 1/ 16W , 1 /2W , 1W , or 2w

字符 W 可以是大写也可以是小写.

The character W may be an upper case or a lower case.

推荐答案

使用 捕获组 如果要捕获子字符串:

Use capture group if you want to capture substring:

$str = "this is string 1 / 4w"; // "1 / 4w" can be 1/ 16W, 1 /2W, 1W, 2w
$str = preg_replace('~^(.*?)(\d+(?:\s*/\s*\d+)?w)~i', "$2 $1", $str);
var_dump($str);

这篇关于如何将一个字符串分成两部分然后以相反的顺序将它们连接为一个新字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆