guys can you help me with my problem regarding search for a string. $var = "81,82,83,84,1460,1461,1462,1463,1464,1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477,1478,1479,1480,1481,1482,1483,1484,1485,1486"; $var2 = "84"; i want to check if the var2 is in the $var I use ereg and i keep on getting the 84 and the 1484 i want a script to only gets the 84 value.
Not that it is significant enough for something trivial like this, a preg_match() is a fair bit faster than using an array for this. if (preg_match("/(^|,)$var2(,|\$)/", $var)) { // Match found } PHP:
Quicker than explode. Quicker for me to code than regular expressions. But not the fastest, or the most elegant: if (!(strpos($var,$var2)===false)) { // string found. } PHP: -Jason
DopeDomains and aRo, read the question again! While both of your code match "84", they also incorrectly match "1484", hence the need for regex as in my solution or exploding the list by commas as nico suggested.
I suppose But you are missing a comma, probably a typo. And tamen, the trailing comma is needed. So it would be: if (!(strpos(",$var,",",$var2,")===false)) { // string found. } PHP:
Its needed if you put a comma on both sides of $var2, not if you only put a comma in front of $var2. But, we are splitting hairs I doubt you could even measure the difference in execution time.
No wait. Thats gibberish. If you put a comma in front of $var, every number will have a comma in front of them. Then you only need a comma in front of $var2
Both vars need to be enclosed in commas, in case the number we're looking for is the first or the last.