Kasuke_Akira Posted March 23, 2007 Share Posted March 23, 2007 I have three options: Stronghold Common Stronghold Uncommon Stronghold Rare I want preg_replace() to search for all three, and replace with the same array so that I don't have to make three seperate array options for each entry but I'm not sure what the search string would look like. Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/ Share on other sites More sharing options...
janroald Posted March 23, 2007 Share Posted March 23, 2007 Difficult to read what you're after m8, but I'll try. I'm guessing what you want requires no more than str_replace, which is much faster then preg_replace $string = ' Stronghold Common Stronghold Uncommon Stronghold Rare'; $replace = array( 'Stronghold Common', 'Stronghold Uncommon', 'Stronghold Rare' } $replace_with = array( '1', '2', '3' } echo str_replace($replace, $replace_with, $string); // output is '1 2 3'; Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213687 Share on other sites More sharing options...
Kasuke_Akira Posted March 23, 2007 Author Share Posted March 23, 2007 Hmm..lets try a different approach..lol I want any instances of Stronghold Common, Stronghold Uncommon, Stronghold Rare to be replaced with: $replace_str[0] = '<img src\'images/sth.gif\'>'; But I don't want to have to do this: $search_str[0] = '/Stronghold Common/'; $search_str[1] = '/Stronghold Unommon/'; $search_str[2] = '/Stronghold Rare/'; $replace_str[0] = '<img src\'images/sth.gif\'>'; $replace_str[1] = '<img src\'images/sth.gif\'>'; $replace_str[2] = '<img src\'images/sth.gif\'>'; I want to do this: $search_str[0] = '/Stronghold [different options]/'; $replace_str[0] = '<img src\'images/sth.gif\'>'; Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213694 Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 Just an adaption of janroalds, why wouldn't you want to do the below? <?php $string = ' Stronghold Common Stronghold Uncommon Stronghold Rare'; $replace = array( 'Stronghold Common', 'Stronghold Uncommon', 'Stronghold Rare' ); $replace_with = '<img src\'images/sth.gif\'>'; echo str_replace($replace, $replace_with, $string); // output is '1 1 1 ?> Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213700 Share on other sites More sharing options...
janroald Posted March 23, 2007 Share Posted March 23, 2007 I guess he's saying that there can be a variety of different "Stronghold _____" 's and he wants a preg to pick them up. If so, the question is really whether there are _really_ many strongholds, or an unforseen amout of them. (dynamically generated maybe) Unless my above sentence has some truth into it, you should go with the solution Hell Toupe and I gave you. Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213706 Share on other sites More sharing options...
Kasuke_Akira Posted March 23, 2007 Author Share Posted March 23, 2007 Here's what an example set of arrays looks like. There are over 100 search strings with corresponding replacement strings: $search_str[0] = '/Time Spiral Common/'; $search_str[1] = '/Time Spiral Unommon/'; $search_str[2] = '/Time Spiral Rare/'; $search_str[3] = '/Stronghold Common/'; $search_str[4] = '/Stronghold Unommon/'; $search_str[5] = '/Stronghold Rare/'; $replace_str[0] = '<img src\'images/tsp c.gif\'>'; $replace_str[1] = '<img src\'images/tsp u.gif\'>'; $replace_str[2] = '<img src\'images/tsp r.gif\'>'; $replace_str[2] = '<img src\'images/sth.gif\'>'; $replace_str[3] = '<img src\'images/sth.gif\'>'; $replace_str[4] = '<img src\'images/sth.gif\'>'; If you notice for the images for TS, they vary based on Rare, Uncommon, or Common However, there are no different images for each rarity of Stronghold, the same image has to be used. but I don't want to have to define 3 different search and image arrays, for the same image. I know you can do stuff like this : [0-9] and it will search for anything between 0 and 9 instead of having to do a separate instance in the array for each number. but I need the same thing to be done with common uncommon and rare so that Stronghold is constant, but one of those other three can be true as well: $search_str[3] = '/Stronghold [Common,Uncommon,Rare]/'; I know that isn't how it works, but I think you may understand what I mean now. Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213757 Share on other sites More sharing options...
janroald Posted March 23, 2007 Share Posted March 23, 2007 Yes, you can do like this in your preg /stronghold (this|that|other)/ which will match "stronghold this" "stronghold that" or "stronghold other" also you can do something like /stronghold (\w+)/ to math "stronghold whatsoever" In both cases you can return and replace either the entire string or just the subpattern with what suits you. If you are quite clever you can make it so that the relation between the string and the image to put in are consistent so that the right picture can be put in for all circumstances. http://www.php.net/manual/en/reference.pcre.pattern.syntax.php Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213775 Share on other sites More sharing options...
Kasuke_Akira Posted March 23, 2007 Author Share Posted March 23, 2007 Ahh, thanks. lol...i was using commas instead of | so i knew it wasnt working..just didnt know wut it should be. thanks Quote Link to comment https://forums.phpfreaks.com/topic/44008-solved-preg_replace-manipulation-help/#findComment-213833 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.