Sasha
Sasha

Reputation: 8705

PHP - check the string if contains some of the characters

I have a string which contains some of the following characters (comma is used as OR):

$car = (=, !=, <, <=. >, =>)

"string $car secondpart"

How can I see which character is used, adn take it in the new variable?

Upvotes: 1

Views: 90

Answers (2)

Jason OOO
Jason OOO

Reputation: 3552

Use this Simple pattern:

preg_match("/!=|<=|=>|>|<|=)"/, $string, $matches)

Demo: https://regex101.com/r/bA3bG0/1

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use this regex:

if (preg_match('/([!<]=|=>?|[<>])/', $car, $m))
   echo $m[1];

$m[1] will contain one of those string you're looking for.

RegEx Demo

Upvotes: 2

Related Questions