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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:26:58+00:00 2026-05-28T15:26:58+00:00

I am beginner for regular expression. so facing following problem. my code is as

  • 0

I am beginner for regular expression. so facing following problem.

my code is as follows.

$originalStr="101 abc 1101 xyz";
preg_match_all('/[0-9]{3,5} /', $originalStr, $matches);
$result=array();
$result=array_unique($matches[0]);
sort($result);
global $wpdb;
for($i=0;$i<count($result);$i++)
{   

    $getName="SELECT Name FROM MyTable WHERE ProductID='".trim($result[$i])."' LIMIT 1";
    $data = $wpdb->get_results($getName);
    $replaceWith="";
    foreach ($data as $obj)
    {
        $replaceWith=$obj->Name;
        $originalStr=str_replace($result[$i], $result[$i]." : ".$replaceWith, $originalStr);        
        break;      
    }   
}
echo $originalStr;

here my purpose is to replace product id by ‘product id : product Name’ but the problem is that as 1101 is having 101 as its sub number so it is replacing it 1101’s string with also 101’s string.

so i am getting array of numbres fine but my problem is with replacement. I want it to be replaced with exact numbermatch only.

  • 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-28T15:26:58+00:00Added an answer on May 28, 2026 at 3:26 pm

    You could use preg_replace_callback instead of manually looping over the result list and replacing it in a second step (with str_replace not being aware of context or which one to replace).

     $newStr = preg_replace_callback('/([0-9]{3,5}) /', "cb_repl", $originalStr);
    
     function cb_repl($match) {
         list ($all, $num) = $match;
    
         db("SELECT Name FROM MyTable WHERE ProductID='$num' LIMIT 1");
         ...
    
         return "{$num} : {$replaceWith} ";   // just the replacement string
     }
    

    So just do your database query in the callback. It’s not super efficient to do multiple queries in a loop, nor in that callback. But it’s easier at least. And preg_replace_callback knows exactly which part it matched and replaces this exact substring with the returned replacement.

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

Sidebar

Related Questions

Came across this handy regular expression utility in Python (I am a beginner in
I am not a beginner to regular expressions, but their use in perl seems
I'm really a beginner when it comes to regular expressions, and I'm not really
Today, I am learning regular expressions =D I understand some basics, like \d+ matches
I'm a beginner using ANTLR. I'm just doing the following for test purposes and
Beginner question. How do I substitute: $_SESSION['personID'] for {personID} in the following: public static
I'm beginner to Regular Expressions. Is there any way to find mismatched point or
Beginner in JS :) needs an explanation of code piece from Crockford's book ,
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner help needed :) I am doign an example form a php book which

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.