Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP: Possible to make value increment in a string?

  • 27-04-2011 8: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