Kevin S Kevin S - 9 days ago 5
PHP Question

Break apart a string between multiple hyphens

Ive got a string that looks like this:

ESD05 - 10000 Research Blvd Svrd Sb - ALARM - Fire Alarm - Thu, 24 Nov 2016 06:35 PM


I'm wondering if its possible to break apart the string based on the hyphens(-) then be able to echo the separate parts. I'd like to break it down like this:

[ESD05]
[10000 Research Blvd Svrd Sb]
[ALARM]
[Fire Alarm]
[Thu, 24 Nov 2016 06:35 PM]

Answer

Use the explode(), example:

$sentence = "ESD05 - 10000 Research Blvd Svrd Sb - ALARM - Fire Alarm - Thu, 24 Nov 2016 06:35 PM ";

$sen = explode(" - ", $sentence);

for ($x = 0; $x < count($sen); $x ++) {
echo $sen[$x] . "<br>";
}
Comments