Reputation: 882
I am new to PHP and don't know much about PHP. I just understand some basics.
I have a PHP
variable called $myVariable
. This variable is filled with strings which I loaded from the internal system. I know how to load the strings from the system and this works so far. So my variable looks like this when I try to echo:
String1 123 Test
String2 456 Test2
String3 789 Test3
etc.
My question now is: How can I work with a single line? I mean if I just want to use Line 1 (String1 123 Test), how can I do that? For example I just want to echo
line 1. I tried this so far:
<?php echo $myVariable; ?>
But this echo the whole variable (what is logical). Have I to work with an array or something like this? If yes, can you give me an example?
Thanks in advance
Upvotes: 0
Views: 87
Reputation: 959
You have to split your variable.
if your want to split when there are a new line use:
$output= preg_split('/\r\n|\r|\n/', $myvariable);
Else you can use the explode function: https://www.php.net/explode
Upvotes: 0
Reputation: 68476
Make use of explode
with PHP_EOL
$str="String1 123 Test
String2 456 Test2
String3 789 Test3";
$newarr = explode(PHP_EOL,$str);
//print_r($newarr);
echo $newarr[0]; // "prints" String1 123 Test
echo $newarr[1]; // "prints" String2 456 Test2
echo $newarr[2]; // "prints" String3 789 Test3
Upvotes: 3
Reputation: 1200
Assuming each line seperated by a new line (if it is not new line replace "\n" with the delimiter)
$pieces = explode("\n", $myVariavle);
echo $pieces[0]; //For your first line
Upvotes: 2
Reputation: 4138
$arr = explode(LINE_SEPARATOR,$myVariable);
so probably its:
$arr = explode("\n",$myVariable);
You can access every line by $arr[INDEX] where INDEX is line no from 0 to count($arr)-1
Upvotes: 3