开发者

Get the separating punctuation in a string with PHP

I have a product that is sold to multiple customers, each customer has its own unique product code derived from the my original product code e.g

My code: 1245-65

Customer 1: 1245/65

Customer 2: 1245.65

开发者_如何学Go

My question: Is there any way to analyse such a string and find what is separating its integers? My goal is to have a settings page where a demo customer code would be entered then all product codes would be derived from that example code. I'm sure PHP can handle this!

EXTRA INFO:

Sorry, I haven't given enough information. There might be a situation where the separator is an alphabetical quantity e.g 1245ABC65. I hate updating a question like this when so many people have given valid answers :( my fault.


You can use a regular expression to find the separator.

$str = '1245/65';
preg_match("/\d+(.)\d+/", $str, $separator);
$separator = $separator[1];


You may want to look for non numeric characters using preg_match_all

preg_match_all('/[^0-9]/', '1245-95', $matches);
print_r($matches);

//Array ( [0] => Array ( [0] => - ) ) in the example

With the updated question, you have to write :

$str = '1245ABC65';
preg_match("/\d+([^0-9]+)\d+/", $str, $separator);
echo $separator = $separator[1];

or

preg_match_all('/[^0-9]+/', '1245ABC95', $matches);
print_r($matches);

//Array ( [0] => Array ( [0] => 'ABC' ) ) in the example


Use preg_split and regular expressions, to search for others characters than numbers.

$separador = preg_split ('/\d/', '1234/65', -1, PREG_SPLIT_NO_EMPTY)
$separador = $separador[0];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜