简体   繁体   English

正则表达式删除字符串的一部分

[英]regular expression to remove part of string

I am trying to instert an integer into a form. 我正在尝试将整数插入表格。 Users are asked to input there promo number which is in the format 12.2-9999. 要求用户输入促销编号,格式为12.2-9999。 I only want to insert 9999 into the column in my database but I am getting -9999 in the columns. 我只想在数据库的列中插入9999,但在列中却得到-9999。 I am not sure if I should use preg_replace or something else here? 我不确定是否应该在这里使用preg_replace或其他东西?

Just use explode() , like so: 只需使用explode() ,就像这样:

list( , $promo) = explode( '-', '12.2-9999');

Now $promo will contain 9999 . 现在$promo将包含9999

以下正则表达式将检查最后一个-并将数字捕获到末尾(根据示例为9999 ):

/-(\d+)$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM