preg_replace() help.

Discussion in 'PHP' started by blueparukia, Jan 14, 2008.

  1. #1
    I have an input named 'addcss', and a string that contains:
    
    $style = '\$addcss = "some text(multiline)"';
    
    PHP:
    So I get this code:

    $addcss = $_POST['addcss']; to retrieve the value from a text area and then run:

    
    <?
    $style = preg_replace('(\$addcss = "#?((.*?))?#";)','\$addcss = "'.$addcss.'";',$style);
    ?>
    
    PHP:
    And it doesn't change, and I have been running this with several other values, and it works with all of them, and the only thing 2 differences are:

    1. This one has a multiline value
    2. The value is retrieved from a textarea - not an input.

    I am really stumped,

    Thanks

    BP
     
    blueparukia, Jan 14, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Have you tried replacing the newline inputs from the textarea to \n's?

    Jay
     
    jayshah, Jan 15, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Well I am not good enough at regexp to do that either.....so help would be appreciated.
     
    blueparukia, Jan 16, 2008 IP
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    OK I have tried this with another textarea, and it is definetly multiline issues, I desperately need help with this as it is holding up the release of an entire script....

    I have tried this:
    
    $style = preg_replace('#(\$addcss = "((.*?))?"; )#esim','\$addcss = "'.$addcss.'";',$style);
    
    PHP:
    Still no luck :(

    SO basically all I want is something to find and replace anything between " and " regardless whether its multiline or not.
     
    blueparukia, Jan 16, 2008 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can you give a specific example of a string you want it to match, and the result you want to get?
     
    SmallPotatoes, Jan 17, 2008 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Well I have recoded and now it is editing another PHP file:

    
    <?php
    $addcss = "Some text here, this string is multiline.
    And user submitted.";
    ?>
    
    PHP:
    And I want to replace it with a user defined variable retrieved from a textarea.

    The string is contained with the variable $style - which is file_get_contents(php file). I just need to change the text bettween:

    $addcss = " and " when the user edits the textarea and submits the form.

    Thanks,

    BP
     
    blueparukia, Jan 17, 2008 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Assuming that I understand what you're trying to do, this should work:

    
    $s = file_get_contents('myfile');
    $s = preg_replace('/\$addcss = ".*[^\\\]"/s', '$addcss = "' . $new_str . '"', $s);
    
    PHP:
    where $new_str is the carefully sanitized value posted in the form. It appears that you are allowing people to post in values that will be executed as PHP code, which is of course extremely dangerous.
     
    SmallPotatoes, Jan 17, 2008 IP
  8. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #8
    Well yes, but is for admin access only (which is fairly secure), and it doesn't really do anything but change variable values.

    Thanks, I'll test it out,

    BP

    EDIT: Kinda works. However it deletes everything in the file below the ";" at the end of that variable :(
    EDIT2: Got it working, thanks
     
    blueparukia, Jan 17, 2008 IP