Solved

Add char to each string element

Posted on 2010-11-26
15
270 Views
Last Modified: 2012-05-10
Hi,

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;

Open in new window

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)

Open in new window


Thanks.
0
Comment
Question by:Zado
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
15 Comments
 
LVL 1

Expert Comment

by:pak_slm
ID: 34216739
Is there Number if test() is fixed, mean  in you string test() present 6 time?
0
 
LVL 8

Author Comment

by:Zado
ID: 34216772
No, it could be any word or number there
0
 
LVL 1

Expert Comment

by:pak_slm
ID: 34216815
i mean count is fixed to 6 or vary.
0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 13

Assisted Solution

by:darren-w-
darren-w- earned 250 total points
ID: 34216852
how about this:

replace this line to :

$string1=(preg_replace_callback("/\([0-9]\)/","addzero", $xx));

add this function:

function addzero($matches)
{
if (strlen($matches)<2){
$matches= "0".$matches;
}
return $matches;
}
0
 
LVL 8

Author Comment

by:Zado
ID: 34216856
vary, it could be:
asdf(4)-0000000(51)-test1(09)-test2(12)-tesssst(33)-test(4)-12345(1)

Open in new window

but always hyphen separated and one or two digits in brackets
0
 
LVL 8

Author Comment

by:Zado
ID: 34216971
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;

Open in new window

It changes this:
test(4)-test(51)-test(9)-test(12)-test(33)-test(4)

Open in new window

into this:
test(0Array)-test(51)-test(0Array)-test(12)-test(33)-test(0Array)

Open in new window

but should be this:
test(04)-test(51)-test(09)-test(12)-test(33)-test(04)

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 34217022
Ok, solution is closer here:

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;

Open in new window


Just need to move opening bracket before zero, it gives this output:
      

test0(4)-test(51)-test0(9)-test(12)-test(33)-test0(4)

Open in new window

0
 
LVL 1

Assisted Solution

by:n4r-c0m
n4r-c0m earned 250 total points
ID: 34217053
    function addzero($m) {
        $m = $m[1];
        if (strlen($m)<2) {
            $m= "(0".$m.")";
        }
        return $m;
    }

    $Str='test(4)-test(51)-test(9)-test(12)-test(33)-test(4)';
    $something_here='';
    foreach(explode('-',$Str) as $xx) {
                    $string1=(preg_replace_callback("/\(([0-9])\)/","addzero", $xx));
                    echo $string1;
    }

Open in new window

0
 
LVL 13

Expert Comment

by:darren-w-
ID: 34217059
$Str='test(4)-test(51)-test(9)-test(12)-test(33)-test(4)';
$patterns=array();
$patterns[0] = "/\({1}[0-9]\)/";
$patternsr=array();
$patternsr[0] = "(0\${1}1)";
print preg_replace($patterns,$patternsr,$Str);

returns

test(01)-test(51)-test(01)-test(12)-test(33)-test(01)
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 34217069
oops thats wrong
0
 
LVL 8

Accepted Solution

by:
Zado earned 0 total points
ID: 34217148
OK, done:

My solution (darren's script changed actually):
function addzero($matches) {
	if (strlen($matches)<6) {
		$matches= "0".$matches[0];
    	foreach(explode('(',$matches) as $yy) {
       		$matches=(preg_replace("/\(/", "", $matches));
        }
  	}
  	return '('.$matches;
}

foreach(explode('-',$Str) as $xx) {
	$string1=(preg_replace_callback("/\([0-9]\)/", "addzero", $Str));
}
echo $string1;

Open in new window


@n4r-c0m solution (corrected):
$string1=(preg_replace_callback("/\(([0-9])\)/","addzero2", $xx)); changed to
$string1=(preg_replace_callback("/\(([0-9])\)/","addzero2", $Str)); that's my fault, I gave it in question.
Plus move echo out from foreach loop.
function addzero2($m) {
        $m = $m[1];
        if (strlen($m)<2) {
            $m= "(0".$m.")";
        }
        return $m;
    }

    foreach(explode('-',$Str) as $xx) {
                    $string1=(preg_replace_callback("/\(([0-9])\)/","addzero2", $Str));
    }
	echo $string1;

Open in new window


Thanks a lot for your help.
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 34217489
Simplified this a little:

<?php
$Str='test(4)-test(51444)-test(9)-test(122)-test(33)-test(1)';
$patterns=array("/\(([0-9])\)/");
$patternsr=array("(0$1)");
print preg_replace($patterns,$patternsr,$Str);
?>

returns: test(04)-test(51444)-test(09)-test(122)-test(33)-test(01)

without callback
0
 
LVL 1

Expert Comment

by:n4r-c0m
ID: 34217584
Great solution!
More simplify :) :

$Str='test(4)-test(51444)-test(9)-test(122)-test(33)-test(1)';
print preg_replace("/\(([0-9])\)/","(0$1)",$Str);

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 34237547
Thanks for help :-)
0
 
LVL 8

Author Closing Comment

by:Zado
ID: 34246245
I found proper solution based on experts comments, I just changed a bit their scripts, but my comment presents actual solution.
Thanks.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question