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

Regular expressions in PHP

Options
  • 06-07-2007 6:42pm
    #1
    Closed Accounts Posts: 2,349 ✭✭✭


    Sorry to post without due research but I have to get this done very quickly and regular expressions are difficult :confused:

    I'm trying to replace a value in a variable that says "$column_x" (where x is a number) with its actual variable called $column_x. There are a bunch of these with different numbers.

    So for example:
    $column_0 = "hello";
    $column_1 = "aloha";
    
    $string = "English: $column_0. Hawaii: $column_1";
    

    So I do:
    ereg_replace("\$column_[:digit:]", ......................

    but don't know how to finish it. Essentially I'll have to refer back to [:digit:] but have no idea how to do that....

    Any takers?


Comments

  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    Do you need to do the ereg_replace? Assignment like you show will expand the variables (as long as they are inside double quotes).
    [PHP]<?php
    $column_0 = "hello";
    $column_1 = "aloha";

    // using double quotes will expand the variables.
    $string = "English: $column_0. Hawaii: $column_1";
    echo $string;
    echo '<br />';
    // Now use single quotes so that variables are not expanded.
    $string = 'English: $column_0. Hawaii: $column_1';
    echo $string, "\n";
    ?>
    [/PHP] This produces:
    English: hello. Hawaii: aloha
    English: $column_0. Hawaii: $column_1
    


Advertisement