0

我有这个字符串:

子类别 19,类别 1

我想从中获得这些价值:

19

1

在两个单独的变量中。

我尝试了很多方法,在互联网上,但我不能用其中任何一个来做。

任何想法 ?

谢谢!:)

4

1 回答 1

0

Considering the format to be:

subcategory[X],category[Y]

the you use explode in order to split the string in two by the ,. Then you'll have two strings in the format subcategory[X] and category[Y] which can be simplified by removing subcategory and category which will return the two numeric values in the string. At this point you just need of the (int) cast.

Here's an example:

$string; // the string
$ar = explode(',', $string);
$x = (int) str_ireplace('subcategory', '', $ar[0]);
$y = (int) str_ireplace('category', '', $ar[1]);

Notice that this is optimized (and don't use REGEX) but will work only with the fixed format provided above. Therefore category12,subcategory4, subcategory24 or subcategory97category90 will trigger errors.

于 2013-04-04T11:35:33.553 回答