xaeryan Posted February 10, 2009 Share Posted February 10, 2009 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. Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/ Share on other sites More sharing options...
genericnumber1 Posted February 10, 2009 Share Posted February 10, 2009 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. Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-758674 Share on other sites More sharing options...
gunabalans Posted February 10, 2009 Share Posted February 10, 2009 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']"; Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-758724 Share on other sites More sharing options...
printf Posted February 10, 2009 Share Posted February 10, 2009 strtr(); or str_replace() <?php $ary = array ( '[FirstName]' =>'Frank', '[LastName]' =>'Smith' ); $str = 'Hello Mr. [LastName], or shall I call you [FirstName]'; echo strtr ( $str, $ary ); ?> Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-758728 Share on other sites More sharing options...
xaeryan Posted February 10, 2009 Author Share Posted February 10, 2009 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! Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-759249 Share on other sites More sharing options...
The Little Guy Posted February 10, 2009 Share Posted February 10, 2009 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]'); Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-759253 Share on other sites More sharing options...
genericnumber1 Posted February 10, 2009 Share Posted February 10, 2009 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 Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-759254 Share on other sites More sharing options...
xaeryan Posted February 11, 2009 Author Share Posted February 11, 2009 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! Link to comment https://forums.phpfreaks.com/topic/144564-best-way-to-replace-values-in-a-string-with-array-values/#findComment-759627 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.