0

I'm struggling to figure out how to use PHP strpos to find a variable in a string. The code below works if I enter a number directly but not if I replace the number with a variable I know to be a single number. No PHP errors present but the conditional renders false. I've been searching all over and am stumped by this.

The $string variable returns "253,254,255".

The $current_page_id variable returns "253".

    $string = "";
    foreach( $page_capabilities as $post):
        $string .= $post->ID.',';
    endforeach;
    if (strpos($string, $current_page_id) !== false) {

        // This works
        //if (strpos($string, '253') !== false) {

        // This does not
        if (strpos($string, $current_page_id) !== false) {
            echo 'true';
        }
    }
Greg
  • 117
  • 2
  • 14
  • 1
    Should work for `25` and `53` too. You might want to rethink your approach to avoid false positives. – mickmackusa Jan 16 '18 at 22:16
  • Is $current_page_id a string ? If not the documentation on strpos http://php.net/manual/en/function.strpos.php says "If needle is not a string, it is converted to an integer and applied as the ordinal value of a character" – Esteban Garcia Jan 16 '18 at 22:18
  • When you say 'The $current_page_id variable returns "253".', what do you mean by 'returns' exactly? Can you show the var_dump output for that variable? There certainly are some problems with using strpos for this, but the variable should work just the same as the literal value. – Don't Panic Jan 16 '18 at 22:26
  • @mickmackusa this is not quite an exact duplicate. And to be honest the answers here are more useful that that on the original dupe. – Martin Jan 16 '18 at 22:27
  • 1
    Another dupe: https://stackoverflow.com/questions/25671461/how-to-detect-the-presence-of-a-given-number-in-a-string and this one : https://stackoverflow.com/questions/1039738/problem-with-strpos-in-php – mickmackusa Jan 16 '18 at 22:34
  • @mickmackusa ok yes the second one of those looks more useful. Bu I guess you can't change your close vote now? `:-/` – Martin Jan 16 '18 at 22:41
  • What? `if (strpos($string, $current_page_id) !== false) {.. if (strpos($string, $current_page_id) !== false) { echo 'true'; } }` – ArtisticPhoenix Jan 16 '18 at 23:36
  • Obviously, if you check it, then check it again, well ... I don't know the logic is just ... not logic. Like checking it once is not good enough? – ArtisticPhoenix Jan 16 '18 at 23:37

4 Answers4

4

You don't need that CSV string to check for the ID you're looking for. You can just do it in the foreach loop where you're currently building the string.

foreach ($page_capabilities as $post) {
    if ($post->ID == $current_page_id) {
        echo 'true';
        break;     // stop looking if you find it
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Thank you! This simplified the situation greatly. I hate when I overcomplicate things. Only reason I didn't mark it as the answer is because another more specifically answered my question and would be more apt to help the next person. I'll be using your solution though! Much appreciated! – Greg Jan 17 '18 at 13:28
  • 1
    You're welcome. Glad I could help a bit. No worries about the accepted answer thing. I don't really care much about the check mark, I just like to answer if I think I can add something useful. – Don't Panic Jan 17 '18 at 16:28
2

You should cast $current_page_id to a string:

if (strpos($string, (string)$current_page_id) !== false) { ...

The following is from php docs:

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

Which means you were comparing "253,254,255" with the character corresponding to 253 in the ASCII table

YouneL
  • 8,152
  • 2
  • 28
  • 50
0

I would suggest you to change your approach. You should search for the exact string value. Because as @mickmackusa suggested, it would find 25 & 53 too

 $page_capabilities = array(array('POSTID' => 253),array('POSTID' => 254),array('POSTID' => 255));
 $current_page_id = '255';

 $key = array_search($current_page_id, array_column($page_capabilities, 'POSTID'));
 if($key !== false) {
    echo 'true';
 }
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Agam Banga
  • 2,708
  • 1
  • 11
  • 18
0

You need to cast the integer to a string.

$postIDs = [55, 89, 144, 233];

$string = implode(',', $postIDs);
$postID = 55;

if(strpos($string, strval($postID)) !== false) {
    echo 'strpos did not return false';
}
Joe P.
  • 547
  • 4
  • 16