PHP - 在大写字母前添加下划线

发布于 2024-11-13 01:46:43 字数 113 浏览 2 评论 0原文

如何替换一组类似于:

SomeText

to

Some_Text

的单词?

How can I replace a set of words that look like:

SomeText

to

Some_Text

?

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

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

发布评论

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

评论(5

鸩远一方 2024-11-20 01:46:43

使用正则表达式可以轻松实现这一点:

$result = preg_replace('/\B([A-Z])/', '_$1', $subject);

正则表达式的简要说明:

  • \B 断言单词边界处的位置。
  • [AZ] 匹配 AZ 中的任何大写字符。
  • () 将匹配项包装在反向引用编号 1 中。

然后我们用 '_$1' 替换,这意味着将匹配项替换为 [下划线 + 反向引用 1]

This can easily be achieved using a regular expression:

$result = preg_replace('/\B([A-Z])/', '_$1', $subject);

a brief explanation of the regex:

  • \B asserts position at a word boundary.
  • [A-Z] matches any uppercase characters from A-Z.
  • () wraps the match in a back reference number 1.

Then we replace with '_$1' which means replace the match with an [underscore + backreference 1]

方觉久 2024-11-20 01:46:43
$s1 = "ThisIsATest";
$s2 = preg_replace("/(?<=[a-zA-Z])(?=[A-Z])/", "_", $s1);

echo $s2;  //  "This_Is_A_Test"

说明:

正则表达式使用两个环视断言(一个向后查找和一个向前查找)来查找字符串中应插入下划线的位置。

(?<=[a-zA-Z])   # a position that is preceded by an ASCII letter
(?=[A-Z])       # a position that is followed by an uppercase ASCII letter

第一个断言确保在字符串开头不插入下划线。

$s1 = "ThisIsATest";
$s2 = preg_replace("/(?<=[a-zA-Z])(?=[A-Z])/", "_", $s1);

echo $s2;  //  "This_Is_A_Test"

Explanation:

The regex uses two look-around assertions (one look-behind and one look-ahead) to find spots in the string where an underscore should be inserted.

(?<=[a-zA-Z])   # a position that is preceded by an ASCII letter
(?=[A-Z])       # a position that is followed by an uppercase ASCII letter

The first assertion makes sure that no underscore is inserted at the start of the string.

月野兔 2024-11-20 01:46:43

最简单的方法是使用正则表达式替换。

例如:

substr(preg_replace('/([A-Z])/', '_$1', 'SomeText'),1);

substr 调用是删除前导 '_'

The simplest way to do this is with a regular expression replacement.

For example:

substr(preg_replace('/([A-Z])/', '_$1', 'SomeText'),1);

The substr call there is to remove a leading '_'

独守阴晴ぅ圆缺 2024-11-20 01:46:43

$result = strtolower(preg_replace('/(.)([AZ])/', '$1_$2', $subject));

转换:

HelloKittyOlolo
Declaration
CrabCoreForefer
TestTest
testTest

为:

hello_kitty_ololo
declaration
crab_core_forefer
test_test
test_test

$result = strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $subject));

Converts:

HelloKittyOlolo
Declaration
CrabCoreForefer
TestTest
testTest

To:

hello_kitty_ololo
declaration
crab_core_forefer
test_test
test_test
喜你已久 2024-11-20 01:46:43
<?php 

$string = "SomeTestString";
$list = split(",",substr(preg_replace("/([A-Z])/",',\\1',$string),1));
$text = "";

foreach ($list as $value) {
    $text .= $value."_";
}

echo substr($text,0,-1); // remove the extra "_" at the end of the string

?>
<?php 

$string = "SomeTestString";
$list = split(",",substr(preg_replace("/([A-Z])/",',\\1',$string),1));
$text = "";

foreach ($list as $value) {
    $text .= $value."_";
}

echo substr($text,0,-1); // remove the extra "_" at the end of the string

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