Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

InspiredPrynce's avatar

PHP String Manipulation

I have a string like this...

"Export Result for DMGS | JSS 1A | English Studies | 2017/2018"

JSS 1A is dynamic, i.e, it changes depending on the data exported. I only want to remove other parts of the string leaving just JSS 1A or whatever it is that will be placed there...

Thanks alot!

0 likes
5 replies
DavidStiller's avatar
Level 2

How about using explode to split the string Into an array at | and then use index 1.

$arr = explode("|", $string); echo $arr(1);

1 like
DavidStiller's avatar

Vice versa merge your array back to a string using implode.

1 like
Ishatanjeeb's avatar

If your others string values are static then you can also use string sunstr function like-


 string substr ( string $string , int $start [, int $length ] )


DavidStiller's avatar

Some corrections, an array index is selected by [ ], not () and the values should be trimmed, to remove the spaces around the |.

$arr = explode("|", $string); echo trim($arr[1]);

Please or to participate in this conversation.