2

I have a predefined pattern for building a string, which should be created regularly throughout the script.

$str="$first - $second @@ $third"; // pattern for building the string
$first="word1";
$second="word2";
$third="word3";
$string= ..?? // string should be built here based on the pattern

Currently, I am using eval to generate the string in place based on the pattern originally defined. However, as this happens occasionally and eval is generally bad, I wish to find another method.

NOTE that the pattern is defined only one time above all codes, and I can edit the pattern of all the script by one line only. Thus, what makes $string should not be touched for any change.

I tried create_function, but needs the same number of arguments. With eval, I can easily change the pattern, but with create-function, I need to change the entire script. For example, if changing the string pattern to

$str="$first @@ $second"; // One arg/var is dropped

eval Example:

$str="$first - $second @@ $third"; // Pattern is defined one-time before codes
$first="word1";
$second="word2";
$third="word3";
eval("\$string = \"$str\";");

create_function Example:

$str=create_function('$first,$second,$third', 'return "$first - $second @@ $third";');
$string=$str($first,$second,$third);
3
  • 1
    Could you give your example with eval please? It's a little difficult to understand what exactly you're trying to do, since the last string declaration looks like it should work for what you want. Commented Nov 6, 2012 at 5:34
  • @Vulcan I added the examples to clarify the issue. Commented Nov 6, 2012 at 5:39
  • Thanks, they helped clarify your question quite a bit, and I've answered the question. Commented Nov 6, 2012 at 6:00

3 Answers 3

3

You can use the string formatting capabilities offered by sprintf or vsprintf.

$format = "%s - %s @@ %s"; // pattern for building the string
$first = "word1";
$second = "word2";
$third = "word3";
$string = sprintf($format, $first, $second, $third);

You can use vsprintf if you wish to pass an array.

$format = "%s - %s @@ %s"; // pattern for building the string
$values = array($first, $second, $third);
$string = vsprintf($format, $values);
Sign up to request clarification or add additional context in comments.

3 Comments

subtle solution, but what if changing the order of variables in the pattern? for example: $str="$second-$first"; then, we need to change the sprintf pattern too throughout the script.
You can specify in the format what argument number to refer to: $str = "%2$s-%1$s". That would place the second (third including the $format itself) argument before the first in the output order.
Now, it's a practical solution. I appreciate your subtle view to mention sprintf, which is the key function for patterning strings, but I blindly ignored.
0

Seems to be rather simple thing to me. Use str_replace() and replace based on patterns

$str="$first$ - $second$ @@ $third$"; // pattern for building the string
$first="word1";
$second="word2";
$third="word3";

$newstr = str_replace('$first$', $first, $str);
$newstr = str_replace('$second$', $second, $newstr);
$newstr = str_replace('$third$', $third, $newstr);

1 Comment

1. The pattern is not always simple, 2. I want to make change only to the pattern, but by your method, I need to modify $newstr throughout the script, and many many times.
0

This might help, not sure.

$str = "_first_ - _second_ @@ _third_"; // pattern for building the string
// _first, _second_... are placeholders for actual values

// this function performs the string building
function buildString( $arr ) {
   global $str;

   $keys = array_keys( $arr );
   $vals = array_values( $arr );

   return eval( str_replace( $keys, $vals, $str ) );
}

while(some condition here) {
    $str = 'A new string'; // you can change your string builiding pattern here

    // this array is built in the fashion ->
    // keys of array are the variables in $str and
    // their actual values are the values of $str
    $arr=array('_first_' => $first, '_second_' => $second, '_third_' => $third);
    echo buildString( $arr );
}

1 Comment

Currently, eval works perfectly. The point is that eval is not a good thing. I want to do this without eval, but your function is based on eval too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.