Pawan Choudhary
Pawan Choudhary

Reputation: 1083

Need help in splitting a string in variable and assign it's parts to an array

One of my variable in my PHP script produce strings like:

'something/test/1'

I want to split that kind of strings through the keyword / and then assign those parts to an array in PHP by using preg_match or something else.
The output should look like:

array(
    '0' => 'something',
    '1' => 'test,
    '2' => '1'
);

Upvotes: 0

Views: 339

Answers (4)

o0'.
o0'.

Reputation: 11863

explode()

<?php
$te="something/test/1";
$ka=explode('/',$te);
print_r($ka);

Upvotes: 2

user554546
user554546

Reputation:

Just use $array=explode('/',$string). The explode function does the splitting and returns the array that you want.

Upvotes: 4

Brian Glaz
Brian Glaz

Reputation: 15676

There is a php function called explode that does exactly what you want. manual here

Upvotes: 4

65Fbef05
65Fbef05

Reputation: 4522

$array = explode('/', $string);

Upvotes: 4

Related Questions