Jump to content

Append Charachters To Array Values


Gorkfu

Recommended Posts

  $old1[] = $word2.',';
  $old2[] = $word2.'.';
  $oldwords = array_merge($old1, $old2);


  $new1[] = $newwords.',';
  $new2[] = $newwords.'.';
  $newwords = array_merge($new1, $new2);


$newtext = str_ireplace($oldwords , $newwords , $oldtext);

 

I have several arrays (Only listed 2 to keep the post simple.) and they all essentially have the same values (words) but with a different character appended to the end.

 

I want to be able to use one array that has the words and then append the characters without modifying the array's values. The goal is to shrink the amount of values down and not need to merge them any more. I couldn't get regexp to work using a preg_replace. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/130142-append-charachters-to-array-values/
Share on other sites

$oldtext is a paragraph or so of content where I have words replaced.

 

$word2 is a word grabbed from $oldtext and then put inside the arrays with the added charachter on the end.

 

The $old and $new variables go through a loop picking up a different word each time.

 

Lets say these are the values for $oldwords array after the merge:

Array ( [0] => word1, [1] => word2, [2] => word3, [3] => word1. [4] => word2. [5] => word3. )

 

For $newwords it will be essentially the same but with links.

 

Instead of having commas and periods in their I want to append them as I run a replace function.

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.