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');
You probably don’t want to have these string literals:
Try
When you use single-quotes, variables are not parsed, so
Vs
The result from:
Would be that each instance of Test1 is replaced with 10, and so on – so:
Update
I see what you are trying to do. When you pass arrays into
str_replaceit processes them in order – so by the time it looks forTest1, Test2you have already replacedTest1with 10. Reverse the order to do what you want…