str_replace only replaces the second array

0

Hello,

I have a problem that I have tried to repair in several ways, but I can not! I've already looked into the str_replace() function in php.net and nothing!

I have the following code:

$cod = '192.168.1.1';
$char = '300';

$arrayFind = array('[cod]', '[char]');
$arrayReplace = array($cod, $char);

for($i = 0; $i < count($arrayFind); $i++) {
$response = str_replace($arrayFind[$i], $arrayReplace[$i], '[cod]/[char]');
}

The str_replace() only replaces [char] , I'm new to PHP and I really like it, but I'm very confused now!

    
asked by anonymous 07.07.2016 / 23:37

1 answer

1
<?php

$cod = '192.168.1.1';
$char = '300';

$arrayFind = array('[cod]', '[char]');
$arrayReplace = array($cod, $char);

$response = str_replace($arrayFind, $arrayReplace, '[cod]/[char]');

//$response = 192.168.1.1/300
    
08.07.2016 / 00:43