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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:29:55+00:00 2026-06-14T10:29:55+00:00

I need to replace a simple text with comma inside to number. CSV File:

  • 0

I need to replace a simple text with comma inside to number.

CSV File: 

Test1
Test1, Test2
Test1, Test2, Test3

php code

$text = "Test1";
$text1 = "Test1, Test2";
$text1 = "Test1, Test2, Test3";

$search = array('$text','$text1','$text2');
$replace = array('10','11','12');
$result = str_replace($search, $replace, $file);
echo "$result";

the result is: “10”,”10, 11″,”10, 11, 12″

but I want to get “10”,”11″,”12″.

This is the final script but on one of this im getting “10, 12”

$text1 = "Test1";
$text2 = "Test2";
$text3 = "Test3";
$text4 = "Test1, Test2, Test3";
$text5 = "Test1, Test2";
$text6 = "Test1, Test3";
$text7 = "Test2, Test3";
$text8 = "Blank";
array($text8,$text7,$text6,$text5,$text4,$text3,$text2,$text1);
array('10','11','12','13','14','15','16','17');
  • 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-14T10:29:56+00:00Added an answer on June 14, 2026 at 10:29 am

    You probably don’t want to have these string literals:

    $search = array('$text','$text1','$text2');
    

    Try

    $search = array($text,$text1,$text2);
    

    When you use single-quotes, variables are not parsed, so

    $text1 = 'Hello';
    $text2 = '$text1';
    echo $text2; // $text1
    

    Vs

    $text1 = 'Hello';
    $text2 = $text1;
    echo $text2; // Hello
    

    The result from:

    Test1
    Test1, Test2
    Test1, Test2, Test3
    

    Would be that each instance of Test1 is replaced with 10, and so on – so:

    10
    10, 11
    10, 11, 12
    

    Update

    I see what you are trying to do. When you pass arrays into str_replace it processes them in order – so by the time it looks for Test1, Test2 you have already replaced Test1 with 10. Reverse the order to do what you want…

    $text = "Test1";
    $text1 = "Test1, Test2";
    $text2 = "Test1, Test2, Test3";
    
    $search = array($text2,$text1,$text); // reversed
    $replace = array('12', '11', '10');// reversed
    $result = str_replace($search, $replace, $file);
    echo $result;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace in a php file some text from DB with preg_replace
I need to write a simple script to replace a block of text in
I need to open a text file and replace a string. I need this
I need to replace a simple string in a minified .js file after a
I need to replace some value in given text using c# preferably using regex
I need to replace a word AAAA in a file using dictionary: dictionary.txt AXF1
I need to replace link index.php?a=2 ,but if it's have & after a=2 then
I have a script that generates a csv file using the following code: header('Content-type:
I have a simple text that is being parsed with PHP. In that text,
I often have text in columns and need to replace some things without clobbering

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.