Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP: Possible to make value increment in a string?

  • 27-04-2011 09:37PM
    #1
    Closed Accounts Posts: 4,001 ✭✭✭


    Hi all

    I hope someone can help me with this.

    I have a string like this in a text file:

    [PHP]
    some random text $i some random text
    [/PHP]

    I read it into a script and assign it to $variable:

    [PHP]
    $variable = "some random text $i some random text";
    [/PHP]

    Question:

    Using $variable, is there any way I could output the following text?

    [PHP]
    some random text 1 some random text
    some random text 2 some random text
    some random text 3 some random text
    some random text 4 some random text
    [/PHP]

    Note I cannot predict what the random text will be, but I know for sure "$i" will be in there somewhere.

    I have tried using something like this, but obviously it will only work for $i specifically, it can't be used to convert "1" into "2", etc.

    [PHP]
    $variable = str_replace("$i", "1", $variable)
    [/PHP]

    Does anyone have any ideas how I can do this?

    Thanks!


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    I'm confused.

    What about

    [php]
    for ($i = 0; $i < 10; ++$i)
    {
    $variable = "some random text $i some random text";
    print $variable."<br />";
    }
    [/php]

    Ah sorry I get you. You could run eval() on the string.
    Look here:
    http://php.net/manual/en/function.eval.php

    [php]
    <?php
    $string = 'cup';
    $name = 'coffee';
    $str = 'This is a $string with my $name in it.';
    echo $str. "\n";
    eval("\$str = \"$str\";");
    echo $str. "\n";
    ?>
    [/php]


  • Closed Accounts Posts: 4,001 ✭✭✭Mr. Loverman


    Sweet, eval() looks like just what I need.

    I had come up with an independent solution using a combination of strpos, str_replace and substr_replace, but it is crazy complicated. I will use eval() instead.

    Cheers!


  • Closed Accounts Posts: 4,001 ✭✭✭Mr. Loverman


    Sorry I think I'm a bit thick. Could you please help me with this?

    If I have the following:

    [php]
    $variable = "some random text $i some random text";

    // The text "some random text $i some random text" was read in from a file using a regular expression
    [/php]

    How do I get the following to work?

    [php]
    $variable = "some random text $i some random text";
    $i = 7;
    $string = eval('return $variable;');
    echo $string;

    // Output is "some random text 7 some random text";
    [/php]

    Maybe I'm just getting confused. I have a feeling what I want to do is easy but I'm over thinking it.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Try this
    [php]
    $variable = 'some random text $i some random text';
    $i = 7;
    $string;
    eval("\$string = \"$variable\";");
    echo $string;

    // Output is "some random text 7 some random text";

    [/php]


  • Registered Users, Registered Users 2 Posts: 35,522 ✭✭✭✭Gordon


    This?

    [php]
    <?php
    $str = 'some random text $i some random text';
    $i = 7;
    eval("\$str = \"$str\";");
    echo $str. "\n";
    ?> [/php]


  • Advertisement
  • Closed Accounts Posts: 4,001 ✭✭✭Mr. Loverman


    Webmonkey wrote: »
    Try this
    [php]
    $variable = 'some random text $i some random text';
    $i = 7;
    $string;
    eval("\$string = \"$variable\";");
    echo $string;

    // Output is "some random text 7 some random text";

    [/php]

    That worked. I love you, I really do! :pac:


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Ha ha thanks. Gordons worked too though ;)

    Good luck.


Advertisement