Jump to content

[SOLVED] Can I use preg_replace for this?


mhuggins

Recommended Posts

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!!

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.