Altec Posted December 8, 2008 Share Posted December 8, 2008 I'm really not sure why this does not work: $tags = array( 'TITLE', 'NAMETO', 'NAMEFROM', 'MESSAGE', ); $data = array( 'hello', 'Steven', 'Steven', 'Hello', ); echo str_replace('{'.$tags.'}', $data, file_get_contents('emailmessage.html')); The contents of emailmessage.html: <html> <head> <title>{TITLE}</title> </head> <body> <p>{NAMETO}, {NAMEFROM}, {MESSAGE}</p> </body> </html> Should that not output an HTML page with the tags replaced? Currently it just outputs the HTML with nothing replaced. Quote Link to comment https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/ Share on other sites More sharing options...
.josh Posted December 8, 2008 Share Posted December 8, 2008 echo str_replace($tags, $data, file_get_contents('emailmessage.html')); Quote Link to comment https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/#findComment-709209 Share on other sites More sharing options...
flyhoney Posted December 8, 2008 Share Posted December 8, 2008 I think the problem is that you are concatenating a string and an array: '{'.$tags.'}' See how that doesn't really make sense? Quote Link to comment https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/#findComment-709216 Share on other sites More sharing options...
cwarn23 Posted December 8, 2008 Share Posted December 8, 2008 It must be a problem with multi-array-replacements so why not append to each array individually. So use the below: <? $tags = array( 'TITLE', 'NAMETO', 'NAMEFROM', 'MESSAGE', ); $data = array( 'hello', 'Steven', 'Steven', 'Hello', ); $id=0; while (isset($tags[$id])) { $tags[$id]='{'.$tags[$id].'}'; $id+=1; } unset($id); echo str_replace($tags, $data, file_get_contents('emailmessage.html')); ?> May not exactly be the fastest but does the job. Quote Link to comment https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/#findComment-709221 Share on other sites More sharing options...
.josh Posted December 8, 2008 Share Posted December 8, 2008 It must be a problem with multi-array-replacements str_replace accommodates mixed data type arguments (string, string | array, string | string, array | array, array). There is no problem with multi-array-replacements. The problem is that he put { } around the array, which is the same problem the code you provided is going to have, because you put { } around your vars, too. There's only 2 reasons why there would be { }. 1, is if he was trying to encase the pattern in a delimiter. Since str_replace looks for an exact match, it does not use delimiters. And even if he were to use for instance preg_replace, which does use delimiters, he still did it wrong. He would need to put them around each individual array element in his $tags array, like so: $tags = array( '{TITLE}', '{NAMETO}', '{NAMEFROM}', '{MESSAGE}', ); But again, str_replace does not use delimiters. 2nd thing is if he were trying to prevent ambiguity. For instance, f you're using a variable inside quotes, for example: "blahblah{$var}blahblah" but that doesn't work any more than trying to echo an entire array. It will just echo 'array' echo $tags; // output: array echo "{$tags}blah"; // output: arrayblah echo $tags[9]; // output: TITLE echo "{$tags[0]}blah"; // output TITLEblah Your code puts { } around each array element. But to str_replace, that's not the same as doing echo "{$tags[$x]}"; Though even if it were, it would be pointless, as there's nothing else in the string to create ambiguity. What str_replace sees that as is "{TITLE}" so it looks for that literal string, which it won't find unless the document in question formats it like that. Which now that I think about it, is a possibility, since he was obviously trying to do something with those brackets. If that is the case, instead of hardcoding an array and then turning around and looping through each element, just hardcode the {}'s in the array elements to begin with Quote Link to comment https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/#findComment-709396 Share on other sites More sharing options...
Altec Posted December 10, 2008 Author Share Posted December 10, 2008 Yes, I figured that out after perusing the manual a bit. I was just trying to replace "tags" inside the HTML document (in effect making an incredibly simple template engine). Quote Link to comment https://forums.phpfreaks.com/topic/136020-solved-simple-string-replace/#findComment-711145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.