PHP issue: Text explode ()

I have a problem with the explode () function. I use this function to blow strings like "Name: Replica", but sometimes there are 2 or more colons (":") in the string, and there is a problem because my script: Example: " Name: replica: replica2: replica3 "

$explode = explode(":", $string);
$query = "INSERT INTO `table` (`field_1`, `field_2`) VALUES ('".$explode[0]."', '".$explode[1]."')";

And I need a solution to this problem. Because when I split the line after the first colon (":"), the second part should be the last part.

Regards, George!

Ps - Sorry for my English.

+3
source share
3 answers

I think you want to use the argument "limit" (third) for explode():

list($attribute, $value) = explode(":", $string, 2);

This will allow you to get only two results.

http://php.net/manual/en/function.explode.php

+6

$limit explode():

$explode = explode(":", $string, 2);

explode() , . , :

$explode = explode(": ", $string, 2);

, , .

+2

edited as suggested by @Jon Nalley. Please note that the restriction (third parameter) is supported only by PHP 5.x

list($attribute, $value) = explode(":", $string, 2);
+1
source

Source: https://habr.com/ru/post/1795380/


All Articles