PHP - Find a string without using arrays or loops

Questions about programming languages and debugging
Post Reply
User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

PHP - Find a string without using arrays or loops

Post by z3r0aCc3Ss »

Hello fellas,

I have been asked a question, and they said it is a very stupid and basic level question.
Let's say we have 10 variables in a PHP page.
$var1 = 'apple';
$var2 = 'guava';
$var3 = 'banana';
$var4 = 'jack fruit';
$var5 = 'mango';
.
.
.
$var10 = 'grapes';

And I have one more variable as $str = 'ck';
I have to find whether the string 'ck' is there in any of the above 10 variables. And if yes, then in which variable it is present.
Condition: You can't use arrays. You can't use any conditions or looping structure. However, you can use any framework/core PHP to find it out.

How do I find it then? Can anyone help me with it?
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: PHP - Find a string without using arrays or loops

Post by bad_brain »

have a look at this one:
http://docstore.mik.ua/orelly/webprog/pcook/ch13_07.htm" onclick="window.open(this.href);return false;

;)
Image

User avatar
CommonStray
Forum Assassin
Forum Assassin
Posts: 1215
Joined: 20 Aug 2005, 16:00
18

Re: PHP - Find a string without using arrays or loops

Post by CommonStray »

strstr() will find the first occurrence of a string (needle) in a string (haystack) and returns part of the haystack that includes the needle to the end of the haystack or false if its not found.

echo "var 1 : " . strstr("ck", $var1);
echo "var 2 : " . strstr("ck", $var2);
echo "var 3 : " . strstr("ck", $var3);
echo "var 4 : " . strstr("ck", $var4);
echo "var 5 : " . strstr("ck", $var5);
echo "var 6 : " . strstr("ck", $var6);
echo "var 7 : " . strstr("ck", $var7);
echo "var 8 : " . strstr("ck", $var8);
echo "var 9 : " . strstr("ck", $var9);
echo "var 10 : " . strstr("ck", $var10);

http://php.net/manual/en/function.strstr.php" onclick="window.open(this.href);return false;" onclick="window.open(this.href);return false;

strpos will find the position of the first occurrence of a string(needle) in a string(haystack) and return its numerical position in the haystack - it is faster if you just want to find out if the needle exists or not.

http://php.net/manual/en/function.strpos.php" onclick="window.open(this.href);return false;" onclick="window.open(this.href);return false;

Post Reply