php正则表达式将字符串后跟数字转换为多个字符串,每个字符串后跟一个数字 [英] php regular expression to convert string followed by number into multiple strings each followed by one of the digits

查看:26
本文介绍了php正则表达式将字符串后跟数字转换为多个字符串,每个字符串后跟一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更换

Apple 123456

Apple 1|Apple 2|Apple 3|Apple 4|Apple 5|Apple 6

通过 php pcre?

by php pcre?

推荐答案

Bogdan 正则表达式的修改版本,使用了负前瞻.

Modified version of Bogdan's regex using negative lookahead.

"number|Apple " 替换数字,除非它是字符串中的最后一个字符.

Replace number with "number|Apple " unless it is the last character in the string.

<?
$string = "Apple 123456";
echo preg_replace('/([0-9])(?!$)/', '$1|Apple ', $string);
?>

输出:Apple 1|Apple 2|Apple 3|Apple 4|Apple 5|Apple 6

这篇关于php正则表达式将字符串后跟数字转换为多个字符串,每个字符串后跟一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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