import java.net.*;
import java.io.*;
import java.util.regex.Pattern;
public class AppDev {
public static void main(String[] args) throws Exception {
URL website = new URL("MyWebsite.php");
BufferedReader in = new BufferedReader(
new InputStreamReader(website.openStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
sb.append(inputLine);
}
in.close();
}
}
I have this code which reads from a php file and displays this output:
Event Number: 1
Date: 2018-05-16
Time: 12:00:00
Event: meeting with friend
Event Number: 5
Date: 2018-05-03
Time: 01:34:37
Event: what the hell
Event Number: 6
Date: 2018-05-03
Time: 01:35:05
Event: Rage at MySQL
Event Number: 16
Date: 2018-06-20
Time: 08:00:00
Event: hfdhfdfgfg
I would like to split each event and its information by its own in an array for example and then output each array in a jtextfield for example the first entry would be
Event Number: 1
Date: 2018-05-03
Time: 01:34:37
Event: meeting with friend
and so on
This trick may help you
Input
$string = 'Event Number: 1Date: 2018-05-16Time: 12:00:00Event: meeting with friendEvent Number: 5Date: 2018-05-03Time: 01:34:37Event: what the hellEvent Number: 6Date: 2018-05-03Time: 01:35:05Event: Rage at MySQLEvent Number: 16Date: 2018-06-20Time: 08:00:00Event: hfdhfdfgfg';
Solution
Add a special element or word that not occur in the string before Event Number and removes the first occurrence of that by ltrim . The str_replace will replace Event Number
to |Event Number
.
I used |
here.
Code is as follows:
$string = ltrim(str_replace('Event Number','|Event Number',$string),'|');
$array = explode('|',$string);//convert the string into array.
echo "<pre>";print_r($array);
Output
Array
(
[0] => Event Number: 1Date: 2018-05-16Time: 12:00:00Event: meeting with friend
[1] => Event Number: 5Date: 2018-05-03Time: 01:34:37Event: what the hell
[2] => Event Number: 6Date: 2018-05-03Time: 01:35:05Event: Rage at MySQL
[3] => Event Number: 16Date: 2018-06-20Time: 08:00:00Event: hfdhfdfgfg
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.