Hump string to underline style

Source: Internet
Author: User

// 驼峰字符串转换成下划线样式$str = ‘openAPI‘;echo $str."<BR>";echo strtolower(preg_replace(‘/((?<=[a-z])(?=[A-Z]))/‘, ‘_‘, $str)).‘<br>‘;
//open_ap

Supplement the regular expression:

?: 不想被捕获的时候使用 可以提高程序执行速度
$string = ‘April 15, 2003‘;$pattern = ‘/(\w+) (\d+), (\d+)/i‘;$replacement = ‘${1}1,$3‘;echo preg_replace($pattern, $replacement, $string); // 结果显示  April1,2003这里就用了反向引用   $replacement  里的 ${1} 代表  (\w+)  ,    $3代表 第2个 (\d+)这个正则表达式 里 一共有 3个 ()  也就是  可以  用 $1  $2  $3  调用 3个()里的 内容。也可以使用 \1    \2    \3 来 代替  $1  $2  $3  都是一样的那接下来 如果把 代码里的 正则表达式 改成下面的$pattern = ‘/(?:\w+) (?:\d+), (\d+)/i‘; 那 这里  只有 一个 () 里的 内容 能使用 带 ?:的 ()里面内容是不会被 捕获的  ,所以 只能使用 $1 代表最后的 (\d+)
?= / ?<=  这个叫断言,只匹配一个位置

比如,你想匹配一个“人”字,但是你只想匹配中国人的人字,不想匹配法国人的人
就可以用一下表达式
(?=中国)人
所以,楼主的表达式与其他通配符连用才能起到效果。
(?=.*[a-z])\d+
这个就表示 匹配以“任意字符连着一个小写字母”开头的数字,只匹配数字。

(?<=exp)这个是放后面的。

Hump string to underline style

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.