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