0
friend_test2_2_Mobile

I have to split this String on the basis of _ in php and acess data according to postions.

How do we split and access these in php

Please Help !!!

user32876
  • 65
  • 2
  • 12
  • Did you make an attempt to solve it? What output do you expect? – anubhava Apr 08 '14 at 16:43
  • possible duplicate of [PHP: Split string](http://stackoverflow.com/questions/5159086/php-split-string) – CanSpice Apr 08 '14 at 16:44
  • I didn't knew how to access these after the explode..!! – user32876 Apr 08 '14 at 16:44
  • 2
    @user32876 It's an array. You can find ***billions*** of places explaining how to use an array in PHP. – h2ooooooo Apr 08 '14 at 16:45
  • 1
    As an aside, please read some of the PHP documentation. It's fairly clear and gives examples on how to use these functions. Also, Google is your friend. Just search for "php split string" and you'll get loads of answers. – CanSpice Apr 08 '14 at 16:50

2 Answers2

0

You want explode:

$data = explode('_', $string);

The results are an array, so you access the elements as you would any other array.

$friend = $data[0]
CanSpice
  • 34,814
  • 10
  • 72
  • 86
0

you're looking for explode.

$parts = explode('_', 'friend_test2_2_mobile');
// $parts = array('friend','test2','2','mobile');
// $parts[0] = 'friend'
// $parts[1] = 'test2'
// $parts[2] = '2'
// $parts[3] = 'mobile'

Or if you want more control, you can use list:

list($name,$foo,$id,$device) = explode('_', 'friend_test2_2_mobile')
// $name = 'friend'
// $foo = 'test2'
// $id = '2'
// $device = 'mobile'
Brad Christie
  • 100,477
  • 16
  • 156
  • 200