Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:39:37+00:00 2026-06-04T01:39:37+00:00

I have a string of variable length. Each character is either ‘r’, ‘w’, or

  • 0

I have a string of variable length. Each character is either ‘r’, ‘w’, or ‘x’.

I know how to use strpos() to find the position of the first occurrence of a character within a string. But I’d like to find the position of the first occurrence of either ‘r’ or ‘w’. Put another way, I’d like to find the position of the first occurrence of “not x”.

For example:

"rwxrxx" would return 0
"wxrr" would return 0
"xxwwwwwr" would return 2
"xxrx" would return 2
"xxxwrrwxxrrwxrrwwxrxrw" would return 3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:39:39+00:00Added an answer on June 4, 2026 at 1:39 am

    The optimal way to do this (although with a small caveat) would be one of strspn() or strcspn(), but there’s a slight difference depending on exactly what you want to do …

    If you want to find the first occurence of a character other than ‘x’, then you need strspn():

    php > $pos = strspn('xxxwrrw', 'x');
    php > var_dump($pos);
    int(3)
    

    And if you need the first occurence of specifically ‘w’ or ‘r’, you’d need strcspn():

    php > $pos = strcspn('xxxwrrw', 'rw');
    php > var_dump($pos);
    int(3)
    

    But as I said, there’s a small caveat … these functions return the initial length that matches or doesn’t match the given mask/needle (the ‘c’ in strcspn() stands for “counter”).
    What this means is that they won’t ever return false and you’ll always get a positive result for e.g. ‘xxxxxxx’:

    php > $pos = strcspn('xxxxxx', 'rw');
    php > var_dump($pos);
    int(6)
    

    Therefore, you may want to check if the returned position does really exist, like this:

    php > $input = 'xxxxxx';
    php > $pos   = strspn($input, 'x'); // i.e. $pos = 6;
    php > var_dump(isset($input[$pos]));
    bool(false) // There was no character other than 'x'
    

    Original answer:

    php >     $string = 'xrwxrxx';
    php >     preg_match('/[^x]/', $string, $matches, PREG_OFFSET_CAPTURE);
    php >     echo $matches[0][1];
    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable length string where each character represents a hex digit. I
I have a string of a variable length and at the end of the
I have a table that contains in one column a variable length delimited string
I have a java string, which has a variable length. I need to put
I have a list of variable length and am trying to find a way
I have a String variable (basically an English sentence with an unspecified number of
I have a string variable in C# aka product_name, I want to filter like
I have javascript string variable with var sttr=We prefer questions that can be answered
I have a String variable say strHTML = <div class='abc'> This is a test
I want to have a string variable for a PHP class, which would be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.