Jump to content

Best way to replace values in a string with array values


xaeryan

Recommended Posts

Description is pretty bad, I know, but not sure how else I could phrase it.  Here's what I've got:

 

$ary = array('FirstName'=>'Frank','LastName'=>'Smith');

 

$str = 'Hello Mr. [LastName], or shall I call you [FirstName]';

 

What's the best way to replace the [] values with the proper array index?

 

Basically, Replace [match] with the value at array[match].  I looked at preg_replace_callback, but there's no good way to send an extra parameter to the callback function.

incredibly messy, but you get the picture...

 

<?php
$ary = array('FirstName'=>'Frank','LastName'=>'Smith');
$str = 'Hello Mr. [LastName], or shall I call you [FirstName]';

echo preg_replace('/\[([A-Za-z0-9_]+)\]/e', '$ary["\\1"]', $str);
// Hello Mr. Smith, or shall I call you Frank

 

edit: cleaned it a little, didn't need to play with its greed.

use simple and straight forward way to get good performance and will not cause error at any stage

 

$ary = array('FirstName'=>'Frank','LastName'=>'Smith');

 

$str = "Hello Mr. $ary['LastName'], or shall I call you $ary['FirstName']";

incredibly messy, but you get the picture...

 

<?php
$ary = array('FirstName'=>'Frank','LastName'=>'Smith');
$str = 'Hello Mr. [LastName], or shall I call you [FirstName]';

echo preg_replace('/\[([A-Za-z0-9_]+)\]/e', '$ary["\\1"]', $str);
// Hello Mr. Smith, or shall I call you Frank

 

edit: cleaned it a little, didn't need to play with its greed.

 

How does this work (which it does, and well)?  Particularly, what is the meaning of the /e?  And in the replace parameter, can you explain the $ary["\\1"] ?  Thanks!

Not sure if this is how you wan't to approach this, but this works for me:

 

function text2bbc($text){
    $find = array(
        '~\[FirstName]~s',
        '~\[LastName]~s'
    );
    $replace = array(
        'Frank',
        'Smith'
    );
    return preg_replace($find,$replace,$text);
}

echo text2bbc('Hello Mr. [LastName], or shall I call you [FirstName]');

How does this work (which it does, and well)?  Particularly, what is the meaning of the /e?  And in the replace parameter, can you explain the $ary["\\1"] ?  Thanks!

 

printf's solution is more efficient if you don't mind reindexing the array slightly.

 

From the manual: "The e modifier makes preg_replace()  treat the replacement  parameter as PHP code after the appropriate references substitution is done."

The \\1 holds the value found between the first (and only in this case) pair of parenthesis.

More info: http://us.php.net/manual/en/function.preg-replace.php

Thanks for all your help so far guys...

Is there any way I can reindex my arrays in this situation:

 

$template = 'Hello Mr. [LastName], or shall I call you [FirstName]?';

$res = mysql_query( 'SELECT LastName, FirstName FROM users' );  // get rows from the database

while ( $one_row = mysql_fetch_assoc( $res ) ) {

      echo ???  // for each row, i want to print Hello Mr ....

}

 

Assume that I CANNOT put the string $template within the while loop as 'Hello Mr. $one_row['LastName']...', as it will be coming from an external source, written by an end user who should require no knowledge of the behind-the-scenes coding (other than a list of available fields they can use, such as [FirstName] and [LastName] ).

 

Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.