Categories
Mastering Development

PHP – Extract Custom Operators from String

I have a MySQL database back-end which is searched via PHP. I’m trying to implement custom operators for detailed searching.
Here is an example:

$string = "o:draw t:spell card";

Where o: = Search by Description and t: = Card Typing. This would be similar to a custom syntax from a site like Scryfall https://scryfall.com/docs/syntax

Here is my work in progress example for o:

$string = "o:draw t:spell card";

    if (strpos($string, 'o:') !== false) {
        $text = explode('o:', $string, 2)[1];
        echo $text;
    }

The output is: draw t:spell card

I’m trying to get the output to respond with just: draw

However, it would also need to be able to accomplish this “smartly”. It may not always be in a specific order and may show up alongside other operators.

Any suggestions on how to build around a system like this in PHP?

Leave a Reply

Your email address will not be published. Required fields are marked *