0

I have a text like this,

$string = "I have some fruits like [name], [another_name] and [one_another_name]";

And an array like this,

$fruits_array = array("Banana", "Apple", "Orange");

Now, how can I replace the texts between braces by following array?

I want a result like this,

I have some fruits like Banana, Apple and Orange.

Please provide the actual solution.

Thanks in advance.

3
  • 1
    What you have tried so far ? Commented Jun 7, 2018 at 6:03
  • @tanmoy-dutta - possible DUPLICATE. check stackoverflow.com/questions/8586141/… Commented Jun 7, 2018 at 6:11
  • Thanks @Sudhir Ojha for your suggestion. But sorry to say that I didn't found in that post what actually I need. Commented Jun 7, 2018 at 6:25

5 Answers 5

3

Try this

$string = "I have some fruits like [name], [another_name] and [one_another_name]";
$fruits_array = array("Banana", "Apple", "Orange");

foreach($fruits_array as $replace)
{
    $string = preg_replace('/\[.*?\]/i', $replace, $string, 1);
}

echo $string;
Sign up to request clarification or add additional context in comments.

6 Comments

sorry it was by mistake
Elegant approch base on regex. Nice pruposal
If we give fullstop . at the end, output is getting weird.
Thanks you all. But sorry to say that it's not working. Do you have any alternative idea?
Do you have different code? If you have then post it, I already tried my answer and its successful
|
1
<?php

$string = "I have some fruits like [name], [name] and [name]";

$fruits_array = array("Banana", "Apple", "Orange");

foreach ($fruits_array as $key => $value) {
  $string = preg_replace('[name]', $value, $string, 1);
}
$string = str_replace('[', '', $string);
$string = str_replace(']', '', $string);
echo $string;

Comments

0

You might also use preg_replace_callback with a regex \[[^]]+\] to match an opening [, then match not a ] using a negated character class and then match ].

In the callback use array_shift to shift an element off the beginning of the array and use that as the replacement.

$string = "I have some fruits like [name], [another_name] and [one_another_name]";
$fruits_array = array("Banana", "Apple", "Orange");

$string = preg_replace_callback(
    '/\[[^]]+\]/',
    function ($matches) use (&$fruits_array) {
        return array_shift($fruits_array);
    },
    $string
);

echo $string;

Demo

2 Comments

@TanmoyDutta I have changed my answer to preg_replace_callback because the previous will not work when there already is %s present in the string.
Thanks @TheFourthBird
0

Implode the array by comma with space. Try like this :

$fruits_array = array("Banana", "Apple", "Orange");

echo 'I have some fruits like '.implode(', ',$fruits_array).'.';

Comments

0

Try this...

$string = "I have some fruits like [name], [name] and [name]";
$arr = explode('[name]',$string);
$fruits_array = array("Banana", "Apple", "Orange");

$newText = '';
for($i = 0; $i < count($arr); $i++){
   $newText .= $arr[$i] . $fruits_array[$i];
}

echo $newText;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.