ggg
ggg

Reputation: 1192

How to compare formated strings in php

i have two strings to compare in PHP. They have same caracters (Uppercase letters separeted by -) but not in the same sort. My problem is that i need to know $a to do my query and i only have $b who is generated by the user.

$a = 'LC-A-T-P-DPE-ELE';     //saved in bdd
$b = 'T-P-DPE-ELE-LC-A';  //generated by the user

$sql = "SELECT * FROM table WHERE col=$a";

Upvotes: 0

Views: 61

Answers (1)

db-mobile
db-mobile

Reputation: 326

You can explode both strings (using '-' as seperator), sort the resulting arrays and then implode back to string. Something similar to this

$arrayA = explode('-', $a);
$arrayB = explode('-', $b);
sort($arrayA);
sort($arrayB);
$aSorted = implode('-', $arrayA);
$bSorted = implode('-', $arrayB);

After that $aSorted and $bSorted should be the same string, if $a and $b are equivalent as per your definition.

Upvotes: 1

Related Questions