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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:19:28+00:00 2026-05-23T07:19:28+00:00

I have a doubt again on RegEx in Php. Assume that I have a

  • 0

I have a doubt again on RegEx in Php.

Assume that I have a line like this

716/52  ; 250/491.1; 356/398; 382/144

I want the output to be

  1. Replace all semi-colon with comma. I think I can do this using

    $myline= str_replace(";", ",", $myline);
    
  2. Interchange the numbers and replace ‘/’ with a comma. That is, 716/52 will become 52,716. This is where I get stuck.

So, the output should be

52,716 , 491.1,250, 398,356, 144,382

I know that using sed, I can achieve it as

1,$s/^classcode:[\t ]\+\([0-9]\+\)\/\([0-9]\+\)/classcode: \2\,\1/

But, how do I do it using preg_match in php?

  • 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-23T07:19:29+00:00Added an answer on May 23, 2026 at 7:19 am
    $str = '716/52  ; 250/491.1; 356/398; 382/144';
    
    $str = str_replace(';', ',', $str);
    
    $res = preg_replace_callback('~[\d.]+/[\d.]+~', 'reverse', $str);
    
    function reverse($matches)
    {
        $parts = explode('/', $matches[0]);
        return $parts[1] . ',' . $parts[0];
    }
    
    var_dump($res);
    

    And working sample: http://ideone.com/BeS9j

    UPD: PHP 5.3 version with anonymous functions

    $str = '716/52  ; 250/491.1; 356/398; 382/144';
    
    $str = str_replace(';', ',', $str);
    
    $res = preg_replace_callback('~[\d.]+/[\d.]+~', function ($matches) {
        $parts = explode('/', $matches[0]);
        return $parts[1] . ',' . $parts[0];
    }, $str);
    
    var_dump($res);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have some doubt about PHP based web crawlers,can it run like the java
This question has been asked again and again, but I have still a doubt.
I have a doubt: - Is there any standard/convention that when should I use
I have little doubt about string reading in C. string reading functions like gets,
I have a question on return and recursive functions. This is again based off
I have the following problem/doubt that I hope somebody could help me with. Let's
I have just started learning PHP, and here's my first doubt... Both of these
I am a tinkerer—no doubt about that. For this reason (and very little beyond
I have a doubt that i have iPhone 3G and iPhone 4 both and
I have a doubt regarding database operation.I have one insert query that should run

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.