Jump to content

Str/preg_Replacing An Array With The Same Words?


jimbbob

Recommended Posts

Okay so I want to replace 2 words with the same words in a different format but only once per occurance.

 

<?php
$text = "Word 1";
$newtext = str_replace(array('Word 1','Word 2'),'Word 1,Word 2',$text);
echo $newtext . "<br>\n";
?>

 

This outputs:

Word 1,Word 1,Word 2

 

I want it to instead out put like this:

Word 1,Word2

Link to comment
Share on other sites

Is it always a comma-separate list like that? explode() out the original string on commas and replace each part if it's in the array of words to replace.

$string = "Word 1";
$words = array("Word 1", "Word 2");
$replace = "Word 1,Word 2";

$array = explode(",", $string);
foreach($array as $k => $word) {
   if (in_array($word, $words)) {
       $array[$k] = $replace;
   }
}
$new_string = implode(",", $array);

Don't know how it compares to a preg_replace() solution.

Link to comment
Share on other sites

thanks for the reply, it seemed to work unitil i tried to change the content of the $string. I'm trying to make a script that automatically replaces synonim words with a group of related synonims so that I can use an article spinner to create different articles.

 

For example the sentence:

 

Toyota is a car. Honda is a vehicle.

 

I want to find any instance of the words "car" or "vehicle" and change it to "car,vehicle".

 

So that the end result is

 

Toyota is a car,vehicle. Honda is a car,vehicle.

Edited by jimbbob
Link to comment
Share on other sites

In light of your last update on what you really want, my previous post was rendered irrelevant. For this you'll need to use Regular Exp<b></b>ressions, which looks something like this:

$Synonyms = array ('car', 'vehicle');
$Find = implode ('|', $Synonyms);
$Replace = implode (',', $Synonyms);
$string = preg_replace ("/$Find/", $Replace, $String);

Note that you might need to use preg_quote () and array_map () on the $Synonyms array before creating the $Find string. To avoid issues with RegExp meta characters.

Edited by Christian F.
Link to comment
Share on other sites

In light of your last update on what you really want, my previous post was rendered irrelevant. For this you'll need to use Regular Expressions, which looks something like this:

$Synonyms = array ('car', 'vehicle');
$Find = implode ('|', $Synonyms);
$Replace = implode (',', $Synonyms);
$string = preg_replace ("/$Find/", $Replace, $String);

Note that you might need to use preg_quote () and array_map () on the $Synonyms array before creating the $Find string. To avoid issues with RegExp meta characters.

 

So I made a small change to your code:

 

<?php
$text = "Toyota is a car. Honda is a vehicle.";
$Synonyms = array ('car', 'vehicle');
$Find = implode ('|', $Synonyms);
$Replace = implode (',', $Synonyms);
$string = preg_replace ("/$Find/", $Replace, $text);
echo $text
?>

 

It outputs: Toyota is a car. Honda is a vehicle.

 

What can I do to make it output: Toyota is a car,vehicle. Honda is a car,vehicle.

 

Im trying to spin articles so thats why I need all synonyms to occur everytime any one of them is mentioned.

Link to comment
Share on other sites

I recommend that you read the PHP manual page for preg_replace (). It will show you how to use the function.

 

I've been trying many things to get it to work.. I've been asking on multiple forums its just im not able get it to limit to only replace the text once and move on.

Link to comment
Share on other sites

You can use the strtr function to replace the words without replacing the words that have been replaced. The only tricky part is creating the right array to feed into the function (the words that you want to be replaced should be keys, the replacement their values).

 

A basic example looks like

 

$text = 'Toyota is a car. Honda is a vehicle.';
echo strtr($text, array('car' => 'car,vehicle', 'vehicle' => 'car,vehicle'));

 

Of course, typing out the array is not ideal so lets create that array from what you already have, by using array_fill_keys.

 

$text = 'Toyota is a car. Honda is a vehicle.';
$synonyms = array('car', 'vehicle');
echo strtr($text, array_fill_keys($synonyms, implode(',', $synonyms)));

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.