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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:20:10+00:00 2026-06-14T08:20:10+00:00

I’ve been working on this for a bit, but my regex is weak. I

  • 0

I’ve been working on this for a bit, but my regex is weak.

I need to check to see if a number is a whole number (single digit) and append a “.001” to it if so. The problem is, it’s in the middle of a line with values separated by commas.

MATERIALS,1,1,9999;1 4PL1 PB_Mel,,1,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1

Needs to be

MATERIALS,1,1,9999;1 4PL1 PB_Mel,,1.001,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1

  1. The line must start with “MATERIALS”.
  2. There are more than one MATERIALS lines.
  3. The value will always be after 5 commas.

I was trying something like this to even replace the number, but I don’t think the approach is quite right:

$stripped = preg_replace('/(MATERIALS)(,.*?){4}(,\d+?),/', '\2,', $stripped);

I tried going through a preg_match_all > for > if process, to at least get the conditional working, but I still have to replace the lines.

EDIT: I forgot the preg_match_all line that proceeded the loop.

preg_match_all('/MATERIALS.*/', $stripped, $materialsLines);
for($i=0;$i<sizeof($materialsLines[0]);$i++) {
            $section = explode(",",$materialsLines[0][$i]);
            if (strlen($section[5]) == 1) {
                $section[5] .= ".001";
            }
            $materialsLines[0][$i] = implode(",",$section);
        }
  • 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-14T08:20:12+00:00Added an answer on June 14, 2026 at 8:20 am

    Why use a regex? You can simply explode the string on comma, check the value in [5], fix it and join the string back together.

    $str = 'MATERIALS,1,1,9999;1 4PL1 PB_Mel,,1,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1';
    $line = explode(',',$str);
    if(strpos($line[5],'.')===false){
        $line[5] .= '.001';
    }
    $str = implode(',', $line);
    

    http://codepad.org/g4r3pLpS

    If reading multiple lines from a file:

    $file = file("someFile.txt");
    foreach($file as $key=>$line){
        $line = explode(',', $line);
        if($line[0] == 'MATERIALS'){
            if(strpos($line[5],'.')!==false){
                $line[5] .= '.001';
                $file[$key] = implode(',', $line);
            }
        }
    }
    file_put_contents("someFile2.txt", implode('',$file));
    

    if for some reason you HAVE to use a regex, this worked for me:

    $str = 'MATERIALS,1,1,9999;1 4PL1 PB_Mel,,1,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1
    MATERIALS,1,1,9999;1 4PL1 PB_Mel,,1.101,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1
    FOO,1,1,9999;1 4PL1 PB_Mel,,1.1,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1
    BLAH,1,1,9999;1 4PL1 PB_Mel,,1,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1
    MATERIALS,1,1,9999;1 4PL1 PB_Mel,,567,6,0.173,0.173,0.375,0,0.375,0,0,0,0,2,0,1,1';
    
    $str = preg_replace('/^(MATERIALS(,[^,]*){4},)(\d+),/m', '$1$3.001,', $str);
    echo $str;
    

    http://codepad.org/l7FfJlDe

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I

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.