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

  • Home
  • SEARCH
  • 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 8720681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:05:32+00:00 2026-06-13T07:05:32+00:00

The user will input a string similar to 151+328 . I need a function

  • 0

The user will input a string similar to 151+328. I need a function to detect the user inputted an operation between two numbers and return them. How to do it? I know how to do the first part with regexes (i.e. [0-9]+\+[0-9], but I don’t have a clue about the second part.

tldr:

function('10+296', '+', 1)='10'    
function('10+296', '+', 2)='296'    
function('perpderpherp', 'derp', 2)='herp'
  • 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-13T07:05:33+00:00Added an answer on June 13, 2026 at 7:05 am

    This function uses a regex to make sure the string is in the following format: [0-9]+[0-9] and then explode to split and return a part:

    function splitAndReturn($str, $delimiter, $part) {
        $pattern = '/^\d+\+\d+$/';
        if (preg_match($pattern, $str)) {
            $parts = explode($delimiter, $str);
            return $parts[$part-1];
        }
        return false;
    }
    
    echo splitAndReturn('10+296', '+', 1); //=10
    echo splitAndReturn('10+296', '+', 2); //=296
    echo splitAndReturn('potatocarrotbanana', 'carrot', 1); // returns false because it doesn't comply with the "detect only [0-9]+[0-9]" rule you stated.
    

    This function uses explode to split the string at the delimiter and return any of the parts:

    function splitAndReturn($str, $delimiter, $part) {
        $parts = explode($delimiter, $str);
        return $parts[$part-1];
    }
    
    echo splitAndReturn('10+296', '+', 1); //=10
    echo splitAndReturn('10+296', '+', 2); //=296
    echo splitAndReturn('potatocarrotbanana', 'carrot', 1); //='potato'
    

    If the delimiter doesn’t exist, it’ll just return the original string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The user should input two strings and the script will performe a text search:
i will need to generate a report based on user input for every food
I want to pass a user input string to a function, using the space
The following program I'm creating will allow the user will input values to create
I'm trying to create a service in which the user will input some text
I am working on semantic web project where user will input text query like
In the program I'm creating, the user will input values to create an array
I´m developing a Book Trading System... The user will input your Book to trading...
Possible Duplicate: raw_input and timeout raw_input() will wait for user input. How to make
Im pulling a text file - it will be input by user currently its

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.