Zado
asked on
Add char to each string element
Hi,
I have this:
I want this output:
Thanks.
I have this:
$Str='test(4)-test(51)-test(9)-test(12)-test(33)-test(4)';
$something_here='';
foreach(explode('-',$Str) as $xx) {
$string1=(preg_replace("/\([0-9]\)/", $something_here, $xx));
}
echo $string1;
So as you can see I need to replace elements inside brackets if there's just one digit, more specifically, I want to add '0' (zero) before this digit. I just need $something_here var, but can't manage with that, please help. Or you can think about different way to do that.I want this output:
test(04)-test(51)-test(09)-test(12)-test(33)-test(04)
Thanks.
Is there Number if test() is fixed, mean in you string test() present 6 time?
ASKER
No, it could be any word or number there
i mean count is fixed to 6 or vary.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
vary, it could be:
asdf(4)-0000000(51)-test1(09)-test2(12)-tesssst(33)-test(4)-12345(1)
but always hyphen separated and one or two digits in brackets
ASKER
Thanks darren-w-, I changed a bit your script and almost works, it adds zero before one digit, but changes this digit into 'Array' in output, that's the only problem.
function addzero($matches)
{
if (strlen($matches)<6){
$matches= "0".$matches;
}
return '('.$matches.')';
}
foreach(explode('-',$Str) as $xx) {
$string1=(preg_replace_callback("/\([0-9]\)/", "addzero", $Str));
}
echo $string1;
It changes this:test(4)-test(51)-test(9)-test(12)-test(33)-test(4)
into this:test(0Array)-test(51)-test(0Array)-test(12)-test(33)-test(0Array)
but should be this:test(04)-test(51)-test(09)-test(12)-test(33)-test(04)
ASKER
Ok, solution is closer here:
Just need to move opening bracket before zero, it gives this output:
function addzero($matches)
{
if (strlen($matches)<6){
$matches= "0".$matches[0];
}
return $matches;
}
foreach(explode('-',$Str) as $xx) {
$string1=(preg_replace_callback("/\([0-9]\)/", "addzero", $Str));
}
echo $string1;
Just need to move opening bracket before zero, it gives this output:
test0(4)-test(51)-test0(9)-test(12)-test(33)-test0(4)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$Str='test(4)-test(51)-tes t(9)-test( 12)-test(3 3)-test(4) ';
$patterns=array();
$patterns[0] = "/\({1}[0-9]\)/";
$patternsr=array();
$patternsr[0] = "(0\${1}1)";
print preg_replace($patterns,$pa tternsr,$S tr);
returns
test(01)-test(51)-test(01) -test(12)- test(33)-t est(01)
$patterns=array();
$patterns[0] = "/\({1}[0-9]\)/";
$patternsr=array();
$patternsr[0] = "(0\${1}1)";
print preg_replace($patterns,$pa
returns
test(01)-test(51)-test(01)
oops thats wrong
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Simplified this a little:
<?php
$Str='test(4)-test(51444)- test(9)-te st(122)-te st(33)-tes t(1)';
$patterns=array("/\(([0-9] )\)/");
$patternsr=array("(0$1)");
print preg_replace($patterns,$pa tternsr,$S tr);
?>
returns: test(04)-test(51444)-test( 09)-test(1 22)-test(3 3)-test(01 )
without callback
<?php
$Str='test(4)-test(51444)-
$patterns=array("/\(([0-9]
$patternsr=array("(0$1)");
print preg_replace($patterns,$pa
?>
returns: test(04)-test(51444)-test(
without callback
Great solution!
More simplify :) :
More simplify :) :
$Str='test(4)-test(51444)-test(9)-test(122)-test(33)-test(1)';
print preg_replace("/\(([0-9])\)/","(0$1)",$Str);
ASKER
Thanks for help :-)
ASKER
I found proper solution based on experts comments, I just changed a bit their scripts, but my comment presents actual solution.
Thanks.
Thanks.