string testing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moehome
Forum Newbie
Posts: 6
Joined: Mon May 13, 2002 10:57 am

string testing

Post 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
!!
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

use a regular expression.....like with the ereg functions

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

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

-enygma
Post Reply