jimbbob Posted October 2, 2012 Share Posted October 2, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/ Share on other sites More sharing options...
requinix Posted October 2, 2012 Share Posted October 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382157 Share on other sites More sharing options...
Christian F. Posted October 2, 2012 Share Posted October 2, 2012 There is a count parameter that you can send to str_replace (), which should be exactly what you're after. Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382158 Share on other sites More sharing options...
jimbbob Posted October 2, 2012 Author Share Posted October 2, 2012 (edited) 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 October 2, 2012 by jimbbob Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382159 Share on other sites More sharing options...
jimbbob Posted October 2, 2012 Author Share Posted October 2, 2012 There is a count parameter that you can send to str_replace (), which should be exactly what you're after. I'm not very advanced in php so can you explain how i can do this. thanks Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382160 Share on other sites More sharing options...
Christian F. Posted October 2, 2012 Share Posted October 2, 2012 (edited) 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 October 2, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382162 Share on other sites More sharing options...
jimbbob Posted October 2, 2012 Author Share Posted October 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382163 Share on other sites More sharing options...
Christian F. Posted October 2, 2012 Share Posted October 2, 2012 I recommend that you read the PHP manual page for preg_replace (). It will show you how to use the function. Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382165 Share on other sites More sharing options...
jimbbob Posted October 2, 2012 Author Share Posted October 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382166 Share on other sites More sharing options...
salathe Posted October 2, 2012 Share Posted October 2, 2012 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))); Quote Link to comment https://forums.phpfreaks.com/topic/268979-strpreg_replacing-an-array-with-the-same-words/#findComment-1382173 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.