note 46929 deleted from function.strstr by betz

From: Date: Wed, 03 Nov 2004 16:39:53 +0000
Subject: note 46929 deleted from function.strstr by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79556@lists.php.net to get a copy of this message
Note Submitter: chris_mdq at yahoo dot com ---- If you want to search some part of a text in a value, I think the note contributed by strstr of 21-Oct-2004 08:03 could be write in the following way: Where is says: "in_str" you have to replace with "strpos" So, the code should be: <?php $needle = '0'; $haystack = '1234567890'; if (strpos($haystack, $needle)) { echo "The haystack contains $needle"; } else { echo "The haystack not contains \"$needle\" on it"; } ?> However, this not take the first character of the haystack, if you want to verify if a text (or part of a text) is on the Haystack , maybe you have to modify the value of the haystack by adding for example a space on it. For example, you can do this: <?php $needle = '1'; $haystack = '1234567890'; $haystack = " ".$haystack; // we add a space at the beginning if (strpos($haystack, $needle)) { echo "The haystack contains a \"$needle\" on \"$haystack\""; } else { echo "The haystack not contains \"$needle\" on \"$haystack\""; } ?> I suppose that this can be useful to somebody (creo que esto puede serle útil a alguien). Abrazos, Christian Valentini, from ARG

« previous php.notes (#79556) next »