SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Fix My Regular Expression (ereg_replace) :(

    Hopefully this is a really simple question, I just have no idea how to write pattern mathching stuff..

    I have the array $main from a mySQL database, containing the row from the DB that was selected. I want to the VALUE of $main[whatever-var-here] from "on" to "checked" -only- if "on" is the -only- word in the value. Is this possible?

    The row has values for diferrent puprposes - text boxes, check boxes, etc.. and I want to replace "on" with "checked" for the check boxes it'll be writing but NOT the word "on" if it shows up in a text box (where other words would be with it).

    PHP Code:
    if ($sql_result) {

       
    $main mysql_fetch_array($sql_result);
    }

    while(list(
    $key$value) = each($main));
    $main preg_replace("/on/""checked"$main ); 
    Thanks!

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, why'd you put ereg in the subject when you're using preg (good ). you scared me.

    first, your while () loop isn't right. but other than that, you want to replace "on" with "checked" when "on" is the ONLY part of the text, right? BTW, you don't need a regular expression. try this:

    PHP Code:
    while (list($key$value) = each($main))
    {
        if (
    $main[$key] == 'on') { $main[$key] = 'checked'; }

    or, i would probably use foreach since it seems to be a bit faster:

    PHP Code:
    foreach ($main as $key => $value)
    {
        if (
    $main[$key] == 'on') { $main[$key] = 'checked'; }

    if you want the "on" to be case-insensitive, you can change the if () to

    PHP Code:
    if (strtolower($main[$key]) == 'on'
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, the good doctor beat me to it and - as usual - is spot on.
    however, i do have a tidbit of information that might justify a reply after all: preg_replace works with arrays as well, so no need to put it in a loop to process elements of an array (although your loop was malformed...)

    so, if you really wanted to go with your way (for whatever reason):
    PHP Code:
    if ($sql_result) {

       
    $main mysql_fetch_array($sql_result);
    }

    $main preg_replace("/on/""checked"$main ); 
    will work...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, very true. however, since he wants to replace if "on" is the only word, the pattern should be changed from '/on/' to '/^on$/'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •