Nav
Nav

Reputation: 115

Want to create a substring when "," encounters in php

I want to create substrings from a string of characters whenever , is encountered in PHP. For example, if Parent string is A, B , C , D then I want to make fours string. String 1 as A, String 2 as B and so on. I am aware of substr function but not sure how to use that so it produces the result I want. Thank you!

Upvotes: 0

Views: 82

Answers (7)

Joe Appleton
Joe Appleton

Reputation: 31

Use explode:

<?php
$string = "A,B,C,D";
$string_array = explode(',',$string);
$stringOne = $string_array[0]; 
$stringTwo = $string_array[1]; 
$stringThree = $string_array[2]; 
$stringFour = $string_array[3]; 
?>

Upvotes: 0

Dipak G.
Dipak G.

Reputation: 725

You can try php's explode function to achieve this.

<?php
  $string = "A,B,C,D"
  explode( ',', $string )
?>

Upvotes: 1

vipulsodha
vipulsodha

Reputation: 634

use explode() function ! Easy to implement !

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

Upvotes: 0

Bob
Bob

Reputation: 436

You can use explode() function like following:

<?php
$string = 'a,b,c';
$array_of_string = explode(',', $string);
echo $array_of_string[0]; // a
echo $array_of_string[1]; // b
echo $array_of_string[2]; // c
?>

Upvotes: 0

StephenCollins
StephenCollins

Reputation: 805

I think you want to try explode().

$string  = "A,B,C,D";
$parts = explode(",", $string);
echo $parts[0]; // A
echo $parts[1]; // B

Upvotes: 0

Mark Reed
Mark Reed

Reputation: 95252

$children = explode( ',', $parent );

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

Upvotes: 1

DonCallisto
DonCallisto

Reputation: 29912

$str = "a,b,c";
$strings_array = explode( ",", $str );
echo print_r( $strings_array );

Upvotes: 0

Related Questions