防止用户在PHP中爆炸字符串

I'm in a situation where I need to explode a string which contains user input but a custom defined delimiter. I want to make sure the user cannot enter that delimiter in their input, so as not to explode the string in the wrong spot.

What is the best way to do this? Is there some type of filter I should run over the user data removing the occurrences of the delimiter? I'd think there would be a better answer than just creating a unique delimiter.

Thanks.

This is an ancient problem, so solve it the way it's already been solved.

In CSV files, the comma is the delimiter, yet entries can contain commas. How? Surround the entries with quotes. What if the entry contains quotes? Double up the quotes on the user input so you know they're data and not delimiters.

Field One,Field Two,"Field, Three","Field ""Four"""

PHP has functions for creating and reading CSV format with custom-defined delimiters, so you don't even have to write the code.

If you don't have str-getcsv in your environment, then as per the comments in your original question.

Assume your delimiter is the pipe symbol

$clientString = 'somestring|fobar';
$clientString = str_replace('|', '{{{somecrazydelimiter}}}', $clientString);

You can the later reverse the situation after exploding it and replace your temporary delimiter with original one:

$clientString = str_replace('{{{somecrazydelimiter}}}', '|', $clientString);