/    Sign up×
Community /Pin to ProfileBookmark

Help with strstr()

Greeting all,

I have a question pertaining to strstr(). Is there anyway that I can use strstr so that it will only look for strings that are at the beginning of a string, and not possible somewhere in the string?

To clarify, I am using strstr to search through a text file, one line at a time, and I want to stop at a specific line that starts with “AC …”. However, my use of strstr is picking up any occurrence of AC in the file. I have a working solution but, it is not very robust.

Here is the code I am using

[CODE]if($switch AND strstr(“$text”, ‘AC’))
{
// $token = strtok($text, ” nt”) ;
// $seq_id = $token[1] ;
// $switch = False ;

$line = str_split($text) ;
$seq_id.$count = $line[5].$line[6].$line[7].$line[8].$line[9].$line[10] ;

// Don’t stop at anymore “AC” strings that may be in file
$switch = False ;
[/CODE]

Later on in the while loop I turn the switch back to “True” so that if another line that begins with AC is present I will begin again.

Does anyone have a suggestion?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 11.2008 — [code=php]if($switch AND strpos("$text", 'AC') === 0)
[/code]
Copy linkTweet thisAlerts:
@ariellOct 11.2008 — The shortest, fastest and "most robust" way might be the usage of Regular Expressions. That way you can specify both the chars/occurrence you watch out for and how many TIMES you'd like to find them.

Best from the south.
×

Success!

Help @TheDragonReborn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...