gerkintrigg Posted March 10, 2011 Share Posted March 10, 2011 Hi everyone! I'm trying to get the variables out of a string like this: $string='this is a sentence with a load of [great, clever, textual] variables '; What I want to do is output a list of strings that have used the variables to create unique strings like this: this is a sentence with a load of great variables this is a sentence with a load of clever variables this is a sentence with a load of textual variables I tried exploding the string into arrays, but what I really need to do is explode on '[' then output the words to an array (until I get to the closing ']') and then move on... The eventual string will have potentially loads of variables, but let's do one thing at a time. What's the best way of starting a project like this? Neil Quote Link to comment Share on other sites More sharing options...
jasonrichardsmith Posted March 10, 2011 Share Posted March 10, 2011 I am not an authority on preg_match but this should be damn close to what you want. if(preg_match('#\[[^)]+\]#', $string, $matches)){ $pieces = explode(", ", $matches[0]); foreach ($pieces as $value) { echo "this is a sentence with a load of $value variables.\n"; } } else{ print "no matches found"; } Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted March 10, 2011 Author Share Posted March 10, 2011 thanks jasonrichardsmith, but I don't seem able to get that to work... I have tried without Preg functions: $str='this is a [good,strong,hard,easy] test, although I think that [you, me, we] may find it difficult to work with it.'; $var_start='['; $arr=explode('[',$str); foreach ($arr as $key => $value) { $split=explode (']',$value); $variables[$key]=$split[0]; #define the variable bank that will be replaced by each variable within the bank: $variable_bank='[good,strong,hard,easy]'; } foreach ($variables as $key => $value) { if($key!=0){ $key--; $var_array[$key] =explode(',',$value); foreach ($var_array[$key] as $key => $value) { echo str_replace($variable_bank,$value,$str).'<br>'; } echo '<br>'; } } It comes close, but doesn't actually output what I need as you can see here: http://manteya.com/test/ It's almost like I need to output the $variable_bank variables as an array together with the $variables to replace the bank with, and then run them all the way through as a for-each type argument. The issue is having more than one variable bank... Quote Link to comment Share on other sites More sharing options...
jasonrichardsmith Posted March 10, 2011 Share Posted March 10, 2011 <?php $string='this is a sentence with a load of [great, clever, textual] variables '; if(preg_match("/[[](.*)[]]/", $string, $matches)){ $pieces = explode(", ", $matches[0]); foreach ($pieces as $value) { $replace = array( '[', ']' ); $value = str_replace($replace,'',$value); echo "this is a sentence with a load of $value variables.<br>"; } } else{ print "no matches found"; } ?> This works but I am guessing there is a more efficient way to not include the [] without doing a str_replace. Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted March 10, 2011 Author Share Posted March 10, 2011 Thanks, it works with the example but it has issues when I use the string in the example below, but I think this is pretty close: $str='this is a [good,strong,hard,easy] test, although I think that [you, me, we] may find it difficult to [use,work with] it.'; $var_start='['; $arr=explode('[',$str); foreach ($arr as $key => $value) { $split=explode (']',$value); $variables[$key]=$split[0]; #define the variable bank that will be replaced by each variable within the bank: if($key!=0){ $variable_bank[$key]=$split[0]; } } foreach ($variable_bank as $key => $value) { $each_var=explode(',',$value); foreach ($each_var as $key2 => $value2) { echo str_replace('['.$value.']',$value2,$str).'<br />'; } } This outputs this is a good test, although I think that [you, me, we] may find it difficult to [use,work with] it. this is a strong test, although I think that [you, me, we] may find it difficult to [use,work with] it. this is a hard test, although I think that [you, me, we] may find it difficult to [use,work with] it. this is a easy test, although I think that [you, me, we] may find it difficult to [use,work with] it. this is a [good,strong,hard,easy] test, although I think that you may find it difficult to [use,work with] it. this is a [good,strong,hard,easy] test, although I think that me may find it difficult to [use,work with] it. this is a [good,strong,hard,easy] test, although I think that we may find it difficult to [use,work with] it. this is a [good,strong,hard,easy] test, although I think that [you, me, we] may find it difficult to use it. this is a [good,strong,hard,easy] test, although I think that [you, me, we] may find it difficult to work with it. I think that if we combined both approaches, we'd probably solve it. Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted March 10, 2011 Author Share Posted March 10, 2011 I sorted it... http://www.manteya.com/spin Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 In: The [lazy, pretentious, honory] professor [stared, glared, ogled] his statistic [students, underlings, minions] like they were [cattle, idiots, aliens]. Some fav's. Out: The honory professor glared his statistic minions like they were cattle. The pretentious professor stared his statistic students like they were aliens. The lazy professor stared his statistic underlings like they were idiots. Lol. good stuff. Quote Link to comment 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.