0

I want to check if a string contains either a % or a .

I've tried it by using strpbrk like this:

$tekens = "€%";
if (strpbrk($tekens, $kortingarray['waarde']) !== TRUE){
    $return[] = array('status' => 'error', 'field' => 'waarde', 'message' => 'Controleer je waarde');
}

But this returns 'status' => 'error'.

What is an easy way to do this?

twan
  • 2,450
  • 10
  • 32
  • 92
  • @WOUNDEDStevenJones That is for all special characters, I only want `€` or `%` to be allowed. – twan Jun 24 '20 at 13:33
  • You can adjust the pattern to only include those 2 characters – WOUNDEDStevenJones Jun 24 '20 at 13:33
  • @twan `strpbrk($tekens, $kortingarray['waarde']) !== TRUE` I don't think the function ever returns true. – nice_dev Jun 24 '20 at 13:43
  • Also, what does `$tekens` and `$kortingarray['waarde']` have in value? – nice_dev Jun 24 '20 at 13:44
  • @vivek_23 `$tekens` always has those two characters in it, I found another SO post that did it this way using `strpbrk`. `$kortingarray['waarde']` can have any value, for example `%test`. The code should return the error status when `$kortingarray['waarde']` contains none of those two characters and succeed (proceed to else statement) when one of them is in `$kortingarray['waarde']`. – twan Jun 24 '20 at 13:45
  • @twan In that case `$kortingarray['waarde']` is your haystack and `$tekens` is the needle of characters to be found. – nice_dev Jun 24 '20 at 13:47
  • @vivek_23 Yes correct – twan Jun 24 '20 at 13:48
  • 1
    @twan Then your condition should be `strpbrk($kortingarray['waarde'],$tekens) === false` – nice_dev Jun 24 '20 at 13:49
  • 1
    @vivek_23 Yes great, that did it. Thank you – twan Jun 24 '20 at 13:51

2 Answers2

1

The arguments to strpbrk are in the wrong order. Also, this function never returns TRUE, it returns a string when a match is found and FALSE when not - see the documentation: https://www.php.net/manual/en/function.strpbrk.php The corrected code would therefore be:

if (strpbrk($kortingarray['waarde'], $tekens) === FALSE) {

Unfortunately this still won't work because strpbrk is not aware of different text encodings - it will only work as expected if you use an 8-bit encoding. Your string is most likely encoded in UTF-8, which means you'll get matches for any string that happens to include any of the 3 bytes used to encode €. For example, is a match.

A working solution is to use preg_match instead:

if (preg_match('/[€%]/u',$kortingarray['waarde']) === 0) {
Joni
  • 108,737
  • 14
  • 143
  • 193
0

A simple check if we can retrieve string position should work in your case.

$yourString = "€%fasdfasdf";

if (strpos($yourString, "€") !== FALSE || strpos($yourString, "%") !== FALSE) {
    //DO SOMETHING
}
  • It needs to be the other way around actually, check if those characters are in there and do something only if they are not. So changing `FALSE` to `TRUE` should be enough right? It's weird because I still get the wrong status back. – twan Jun 24 '20 at 13:43
  • No the FALSE part must stay. You need to negate the whole thing. Check the link I posted – Dharman Jun 24 '20 at 13:46
  • Dude read this statement !== FALSE is NOT EQUALS FALSE so therefore it means TRUE – Edvinas Platovas Jun 24 '20 at 14:13
  • @twan To clarify, if you need the logic to be the inverse, you could either negate the value of the conditional `if (!(strpos($yourString, "€") !== FALSE || strpos($yourString, "%") !== FALSE)) {`, or apply that `!` ("not") to each value separately `if (strpos($yourString, "€") === FALSE && strpos($yourString, "%") === FALSE) {`, or leave the conditional logic as-is and add an `else` clause and add your `//DO SOMETHING` there instead. These all give you the same functionality, it's just a matter of if you do something when the strings _are_ found or when they're _not_ found. – WOUNDEDStevenJones Jun 24 '20 at 23:32