Peter Stuart
Peter Stuart

Reputation: 2434

How do I get the characters inbetween two characters within a string in PHP

I have an file. Each value looks like this:

Peter | Stuart | 10293 | 13/02/93 | [email protected] | [data6] | [data7] | [data8]

My question is, I want data 6, 7 and 8 to be in seperate variables.

So the variable would be like this (in PHP code)

$data6 = [data6]
$data7 = [data7]
$data8 = [data8]

Does that make sense? I understand a loop will be involved, it is more getting the data in between the "|" symbol I am not 100% sure about.

Thanks Peter

Upvotes: 0

Views: 57

Answers (2)

kennypu
kennypu

Reputation: 6051

you can use explode:

$data_array = explode("|",$your_string);

then you can access those values like any other array:

$data_array[5]; // this would be data 6

Upvotes: 3

tradyblix
tradyblix

Reputation: 7569

Use PHP explode. In this case you would iterate to each line and split the string by the pipe | and refer to the values you need by their keys e.g. data[6] would be in index 5 of the array generated by explode like $data[5].

Upvotes: 3

Related Questions