-2

I want to replace a text as below.

$text = '<!DOCTYPE html>
<html>
<head>
</head>
<body>
<pre><code>[[EXAMPLE]] this is a text. <br />[[EXAMPLE2]]</code></pre>
<p style="text-align: center;"><br /><br /></p>
</body>
</html>';


$products['type'] = 1;
$products['quantity'] = 2300.0;
$products['grade'] = null;

 $searchVal = array(
'[[EXAMPLE]]'
'[[EXAMPLE2]]'
);

 $replaceVal = array(
'replace word',
 $products
); 

$text = str_replace($searchVal, $replaceVal, $text);

I did not run this code. This fail is "Array to string conversion"

How can i do this.

Hal1l
  • 3
  • 6
  • 1
    Please tag Programming language used. – Barbaros Özhan Nov 17 '19 at 20:00
  • The message is clear: You can't use an array as a string, maybe you only need an element from that array, something like: $products['name'] – Triby Nov 17 '19 at 20:07
  • I want to use multiple. Because I'm going to parse [[EXAMPLE2]] on the html page. I guess it doesn't work the way I want. Do I have to use a single? – Hal1l Nov 17 '19 at 20:13

1 Answers1

0

According to the documentation:

If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject

Make sure both $searchVal and $replaceVal are linear arrays, currently they are not.

Your $searchVal definition lacks a comma.

Your $replaceVal is an array where the second element is another array.

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26