Assuming i have

$a='1024,1025,0000|1020,0000|';
$b='1024,1025,0000,1020,0000,';

i want replace commas (,) in $b with (|) so that $b is equal to $a like this

$b='1024,1025,0000|1020,0000|';

if i use

$ob=str_replace("0000,","0000|",$b);
echo $ob;

output works :

1024,1025,0000|1020,0000|

But, how to make it with PHP where did i put that this refers to the $a and what if it does not always 0000, with other purposes may be i want replace character referring to another character sequence in the array, so that $b is equal to $a, other sample :

eg :

$a='1024,1025,1234|1020,0000|';
$b='1024,1025,1234,1020,0000,';

output:

$b='1024,1025,1234|1020,0000|';

thanks to your help.

regards,

Member Avatar for LastMitch

But, how to make it with PHP where did i put that this refers to the $a and what if it does not always 0000, with other purposes may be i want replace character referring to another character sequence in the array, so that $b is equal to $a, other sample :

OK so you want $a array to insert into $b array so the output is in $b array which has $a array data in it?

There are 3 things that is happening:

You mention that $a array might not be always 0000?
You want to replace an regex in an array with a different regex.
You want $b array data to become $a array data when it is echo out.

Maybe you can explain it a ittle more what you are doing?

@LastMitch,
i want $b array data to become $a array data when it is echo out. But in my case, I want that $b will be having the sequence (,) and (|) as $a.
in focus is (,) and (|) position,
another example of data

$a='1024,1025,1234|1020,0000|';
$b='1024,1025,3321,1020,2345,';

output :

$b='1024,1025,3321|1020,2345|';

problem has been solved, I am trying to find the sequence (|) at $a with strpos and $b will change in the sequence (|) is found in $a and then i replace it, i'm using this function for replace

function replace_element($str,$search,$replace,$num) { $num = $num - 1; $pieces = str_split($str); if($pieces[$num] == $search) { $pieces[$num] = $replace; } return implode('',$pieces); }

thanks for attention

Member Avatar for diafol

OK I see it's solved - but I'd use regex:

$replace = "|";
$pattern = "\|";

$a = "1024,1025,1234|1020,0000|qufn|";
$b = "1094,1000,2340,0019,0000,what";

preg_match_all("/$pattern/", $a, $matches, PREG_OFFSET_CAPTURE);
foreach($matches[0] as $v) $b = substr_replace($b, $replace, $v[1], 1);
echo $b;

You could sort out the $replace and $pattern to be the same if you take care with the backslashing.

function returnFormat($template, $subject){
    preg_match_all("/\|/", $template, $matches, PREG_OFFSET_CAPTURE);
    foreach($matches[0] as $v) $subject = substr_replace($subject, "|", $v[1], 1);
    return $subject;  
}

You can wangle it to include a dynamic replace character.

Member Avatar for LastMitch

problem has been solved, I am trying to find the sequence (|) at $a with strpos and $b will change in the sequence (|) is found in $a and then i replace it, i'm using this function for replace

I apologized not replying back soon it's good that you solve it.

@diafol, your idea is very good, I like your code, more concise and works well, Thank you for your submission.
@lastmitch, it's okay guys, thank you for your attention,

regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.