Page 1 of 1

string testing

Posted: Wed May 15, 2002 7:47 am
by moehome
hello,
Can anybody tell me how to test a string, unsing jokers;
eg I want to find word which start with "moe" and are 5 characters long!
OR I'd like to find some which end with "t";

thx a lot
!!

Posted: Wed May 15, 2002 8:03 am
by mikeq
I think the StrPos() function will find a substring in a string and will return an integer specifying the position. Now as strings are an array of characters if your substring appears at the start it will return 0.

So when doing an equality check use 3 equal signs so that it evaluates exactly to 0 as an integer, as if the substring is not found it returns false.

$StrPos = StrPos(Haystack,needle);
$LengthMyString = StrLen($MyString);

if (($StrPos===0) and ($LengthMyString==5)){

....

}

You could use StrRPos() to find the last occurrence of 't' and if this is the same as the string length, then you've found strings which end with 't'. Just add this in as an else if to your logic.

Mike

Posted: Wed May 15, 2002 8:04 am
by enygma
use a regular expression.....like with the ereg functions

ereg("^moe[a-zA-Z0-9]{2}",$string);

ereg("t$",$string");

-enygma