note 46929 added to function.strstr

From: Date: Wed, 27 Oct 2004 22:58:05 +0000
Subject: note 46929 added to function.strstr
Groups: php.notes 
Request: Send a blank email to php-notes+get-79220@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.strstr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46929 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46929&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46929&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46929&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46929&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46929&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46929&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46929&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#79220) next »