Yeak
Yeak

Reputation: 2538

PHP replace string with values from array

I have a string such as:

   Hello <%First Name%> <%Last Name%> welcome

and I have a array

 [0] => Array
    (
        [First Name] => John
        [Last Name] => Smith
    )

What I need to do is take the string and replace the words in <% with the actual text from the array

So my output would be

   Hello John Smith welcome

Im not sure how to accomplish this but I cant even seem to replace it with regular text

$test = str_replace("<%.*%>","test",$textData['text']);

Sorry I should of mentioned that the array keys may vary as well as the <%First Name%>

so it could even be <%city%> and the array can be city=>New York

Upvotes: 8

Views: 21943

Answers (8)

Havenard
Havenard

Reputation: 27874

$string = "Hello <%First Name%> <%Last Name%> welcome";
$matches = array(
    'First Name' => 'John',
    'Last Name' => 'Smith'
);

$result = preg_replace_callback('/<%(.*?)%>/', function ($preg) use ($matches) { return $matches[$preg[1]] ?? $preg[0]; }, $string);            

echo $result;
// Hello John Smith welcome

Upvotes: 3

Erdem KO&#199;HAN
Erdem KO&#199;HAN

Reputation: 11

function temp_tag_replace($tag_start,$tag_end,$temp_array,$text){
      if(is_array($temp_array)){
      $keys=array_keys($temp_array);
        foreach ( $keys as $key){
        $val=$temp_array[$key];
        $key=$tag_start.$key.$tag_end;
        $text=str_replace($key,$val,$text);
        }
      }
      return $text;
    }

    $text='Hi %*Name*% %*Surname*%';
    $temp_array=array('Name'=>'Otto','Surname'=>'Man');
    $tag_start='%*';
    $tag_end='*%';

    echo temp_tag_replace($tag_start,$tag_end,$temp_array,$text);

Upvotes: 1

Krish R
Krish R

Reputation: 22711

Can you try this,

    $string ="Hello <%First Name%> <%Last Name%> welcome";
    preg_match_all('~<%(.*?)%>~s',$string,$datas);
    $Array = array('0' => array ('First Name' => 'John', 'Last Name' => 'Smith' ));
    $Html =$string;
    foreach($datas[1] as $value){           
        $Html =str_replace($value, $Array[0][$value], $Html);
    }
    echo str_replace(array("<%","%>"),'',$Html);

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can use this:

$result = preg_replace_callback('~<%(First|Last) Name)%>~', function ($m) {
    return $yourarray[$m[1] . ' Name']; } ,$str);

or much simple (and probably more efficient), use Brian H. answer (and replace search strings by <%First Name%> and <%Last Name%>).

Upvotes: 1

Xyz
Xyz

Reputation: 6013

$array = array('<%First Name%>' => 'John', '<%Last Name%>' => 'Smith');
$result = str_replace(array_keys($array), array_values($array), $textData['text']);

Upvotes: 24

RafaSashi
RafaSashi

Reputation: 17205

echo ' Hello '.$array[0][First Name].' '.$array[0][Last Name].'  welcome';

Upvotes: 0

demonking
demonking

Reputation: 2654

You could use str_replace

$replacedKeys = array('<%First Name%>','<%Last Name%>');

$values = array('John','Smith');

$result = str_replace($replacedKeys,$values,$textData['text']);

Upvotes: 2

Brian H.
Brian H.

Reputation: 505

You can use an array for both the search and replace variables in str_replace

$search = array('first_name', 'last_name');
$replace = array('John', 'Smith');

$result = str_replace($search, $replace, $string);

Upvotes: 6

Related Questions