Jump to content

Strip out variables


gerkintrigg

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/230217-strip-out-variables/
Share on other sites

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";
}

Link to comment
https://forums.phpfreaks.com/topic/230217-strip-out-variables/#findComment-1185614
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/230217-strip-out-variables/#findComment-1185621
Share on other sites

<?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.

Link to comment
https://forums.phpfreaks.com/topic/230217-strip-out-variables/#findComment-1185650
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/230217-strip-out-variables/#findComment-1185686
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/230217-strip-out-variables/#findComment-1185935
Share on other sites

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.