mhuggins Posted December 6, 2007 Share Posted December 6, 2007 I have a situation where I have a text variable like this: var $sentence = "_ went to the _ and _ a _. I also have an array with the same number of elements as there are blanks (underscores) in the above sentence. Something like this: var $words = array('George', 'store', 'bought', 'sandwich'); Normally I could use preg_replace to replace the blanks with a string of text, but this is typically assuming the text you're replacing with will always be the same. Since the text I'm replacing with changes from match to match, I'm not sure how to approach this. I appreciate any suggestions! Edit: Sorry mods, I put this in the wrong forum. If someone could move it, I'd appreciate it. Sorry!! Quote Link to comment Share on other sites More sharing options...
JacobYaYa Posted December 6, 2007 Share Posted December 6, 2007 Are you saying the sentence and the words in the array will be different every time? Quote Link to comment Share on other sites More sharing options...
mhuggins Posted December 6, 2007 Author Share Posted December 6, 2007 Are you saying the sentence and the words in the array will be different every time? The sentence will be the same, it's the blanks in the one sentence I'm replacing. The words will vary from blank to blank. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 If you made each blank different, you could use str_replace. You could number each blank like this _1_ went to the _2_ and _3_ a _4_ Then the code would look like this <?php $phrase = "_1_ went to the _2_ and _3_ a _4_"; $replace = array("_1_", "_2_", "_3_", "_4_"); $replacer = array('George', 'store', 'bought', 'sandwich'); $newphrase = str_replace($replace, $replacer, $phrase); echo $phrase; echo '<br>'.$newphrase; ?> Quote Link to comment Share on other sites More sharing options...
mhuggins Posted December 6, 2007 Author Share Posted December 6, 2007 I thought of that, but I was hoping to do it without numbering the blanks if possible. If I have to, I will probably end up doing it that way though. Thanks for the input Quote Link to comment Share on other sites More sharing options...
effigy Posted December 6, 2007 Share Posted December 6, 2007 <pre> <?php $sentence = "_ went to the _ and _ a _."; $words = array('George', 'store', 'bought', 'sandwich'); function callback () { global $words; static $index = -1; ++$index; return $words[$index]; } echo preg_replace_callback('/_/', 'callback', $sentence); ?> </pre> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 6, 2007 Share Posted December 6, 2007 This function may be an embarrassment compared to effigy's method, but here it is. <?php function transform($phrase, $replacer){ $phrase = explode(' ', $phrase); $i=0; foreach ($phrase as $word){ if ($word == '_'){ $word = str_replace('_', $replacer[$i], $word); $i++; } $newphrase .= $word.' '; } return $newphrase; } ?> To use <?php $phrase = "_ went to the _ and _ a _"; $replacer = array('George', 'store', 'bought', 'sandwich'); $var = transform($phrase, $replacer); echo $var; ?> Quote Link to comment Share on other sites More sharing options...
mhuggins Posted December 6, 2007 Author Share Posted December 6, 2007 @effigy - It's funny you mentioned that possibility, as I actually stumbled across preg_replace_callback yesterday and thought of the exact same solution. I haven't implemented it though since I may need to do the same thing with a second sentence, which would incorrectly offset the array index the second time around. @pocobueno1388 - Unfortunately I can't use that, as it won't work when I come across punctuation. For example, the end of the sentence in my original post ended with "_.", and your method would not replace it correctly. Quote Link to comment Share on other sites More sharing options...
effigy Posted December 6, 2007 Share Posted December 6, 2007 I haven't implemented it though since I may need to do the same thing with a second sentence, which would incorrectly offset the array index the second time around. How about using another global, similar to how $words is working? Quote Link to comment Share on other sites More sharing options...
mhuggins Posted December 6, 2007 Author Share Posted December 6, 2007 I suppose that could work too. Also, I'll use /\b_\b/ in case other words contain underscores for any reason. Quote Link to comment 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.