I want to split my string 192.168.1.1/24
by forward slash using PHP function preg_split.
My variable :
$ip_address = "192.168.1.1/24";
I have tried :
preg_split("/\//", $ip_address);
//And
preg_split("/[/]/", $ip_address);
Error message : preg_split(): Delimiter must not be alphanumeric or backslash
I found the following answer here in stackoverflow Php preg_split for forwardslash?, but it not provide a direct answer.
Use the str. rsplit() method to split the string on a slash, from the right. Get the list element at index 1 . The method will return a new string that only contains the part after the last slash.
Just use another symbol as delimiter
$ip_address = "192.168.1.1/24";
$var = preg_split("#/#", $ip_address);
print_r($var);
will output
Array
(
[0] => 192.168.1.1
[1] => 24
)
This is another way that meet up your solution
$ip_address = "192.168.1.1/24";
$var = preg_split("/\//", $ip_address);
print_r($var);
Output result
Array(
[0] => 192.168.1.1
[1] => 24
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With