I have a string like this:
house1- a normal house
house2-an office
house3-a scholl
There are about 2000 lines. I want to get only house1,house2,house3 etc. from that it and put it in another file. Like:
house1
house2
house3
I understand that the explode function works for separating strings, but how can I do this?
$fContents = file( 'path/to/your/file' );
foreach ( $fContents as $row )
file_put_contents( 'path/to/other/file', explode( '-', $row )[0] , FILE_APPEND );
Shitty but working example (assuming you got 5.4+).
for PHP 5.3:
$fContents = file( 'path/to/your/file' );
foreach ( $fContents as $row )
{
$firstField = explode( '-', $row );
file_put_contents( 'path/to/other/file', $firstField[0] , FILE_APPEND );
}
$str = "house1- a normal house";
$result = explode("-", $str)[0];
var_dump($result);
Assuming the latest PHP version (5.4.x).