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 7528411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:22:23+00:00 2026-05-30T04:22:23+00:00

Consider the following array which holds all US stock tickers, ordered by length: $tickers

  • 0

Consider the following array which holds all US stock tickers, ordered by length:

$tickers = array('AAPL', 'AA', 'BRK.A', 'BRK.B', 'BAE', 'BA'); // etc...

I want to check a string for all possible matches. Tickers are written with or without a “$” concatenated to the front:

$string = "Check out $AAPL and BRK.A, BA and BAE.B - all going up!";

All tickers are to be labeled like: {TICKER:XX}. The expected output would be:

Check out {TICKER:AAPL} and {TICKER:BRK.A} and BAE.B - all going up!

So tickers should be checked against the $tickers array and matched both if they are followed by a space or a comma. Until now, I have been using the following:

preg_replace('/\$([a-zA-Z.]+)/', ' {TICKER:$1} ', $string);

so I didn’t have to check against the $tickers array. It was assumed that all tickers started with “$”, but this only appears to be the convention in about 80% of the cases. Hence, the need for an updated filter.

My question being: is there a simple way to adjust the regex to comply with the new requirement or do I need to write a new function, as I was planning first:

function match_tickers($string) {
  foreach ($tickers as $ticker) {
    // preg_replace with $
    // preg_replace without $
  }
}

Or can this be done in one go?

  • 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-05-30T04:22:24+00:00Added an answer on May 30, 2026 at 4:22 am

    Just make the leading dollar sign optional, using ? (zero or 1 matches). Then you can check for legal trailing characters using the same technique. A better way to go about it would be to explode your input string and check/replace each substring against the ticker collection, then reconstruct the input string.

     function match_tickers($string) {
          $aray = explode( " ", $string );
          foreach ($aray as $word) {
               // extract any ticker symbol
               $symbol = preg_replace( '/^\$?([A-Za-z]?\.?[A-Za-z])\W*$/', '$1', $word );
               if (in_array($symbol,$tickers)) { // symbol, replace it
                   array_push( $replacements, preg_replace( '/^\$?([A-Za-z]?\.?[A-Za-z])(\W*)$/', '{TICKER:$1}$2', $word ) );
               }
               else { // not a symbol, just output it normally
                   array_push( $replacements, $word );
               }
           }
           return implode( " ", $replacements );
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following array, which is claimed to have represented a binary tree: [1,
Consider the following function which fills an array with strings(questions): global $questions; function printQuestions($lines){
Consider the following code fragment in VS2010 Beta 1: let array = Array2D.zeroCreate 1000
Consider the following scenario . I have an array of numbers: [ 1,2,3,4 ]
Consider the following code which is to be thrown at an AR find: conditions
Consider the following piece of code: $tests = array( array (a, b, c), array
consider the following PHP code: <?php $searchsport = $_REQUEST['sport']; $sportarray = array( Football =>
I just noticed a strange behavior which looks like a bug. Consider the following
Consider the following convenient looping idiom. import numpy print shape of x = numpy.array([['a',
Consider the following image: My app first finds all the blue pixels and also

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.