Results 1 to 7 of 7
  1. #1
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,391

    Default PHP preg_replace setting variables

    Is it possible to set a variable using preg_replace in PHP?

    So whenever 'whatever' is found set $variable = 1 and replace the 'whatever' with 'blah'... or something to that effect
    PHP Code:
    preg_replace("/whatever/e","$variable = 1; echo 'blah';",$text); 
    I know the 'e' flag lets you use a php variable as replace string, however it won't let you run PHP code... or will it? Is there a better way to do what I'm wanting?

    What I'm really wanting is to stop other preg_replace from happening if $variable = 1, so I'd put those into an if $variable == 0 etc. and when another condition is satisfied the $variable is set back to 0 so the other $preg_replace can run. These are all run within a while loop

    I'm not sure even I understand what I just wrote

    Cheers,
    Mike.
    what are you doing looking in here?

  2. #2
    Gone Erayd's Avatar
    Join Date
    Dec 2004
    Location
    Wellington, NZ
    Posts
    5,761

    Default Re: PHP preg_replace setting variables

    Quote Originally Posted by Mike View Post
    I know the 'e' flag lets you use a php variable as replace string, however it won't let you run PHP code... or will it?
    That's the entire point of the 'e' flag - it will .

    Is there a better way to do what I'm wanting?
    If you've correctly described your problem, it sounds like preg_replace_callback would be a smarter solution. Is there any reason why you can't use that?
    If you are interested in reading fanfiction on a mobile device or ebook reader, please visit flagfic.com.

  3. #3
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,391

    Default Re: PHP preg_replace setting variables

    Quote Originally Posted by Erayd View Post
    That's the entire point of the 'e' flag - it will
    doesn't seem to let me... what am I doing wrong? For example
    PHP Code:
    $variable 0
    preg_replace
    ("/whatever/e","$variable = 1; echo 'blah';",$text); 
    seems to return '0 = 1' rather than just set $variable = 1...

    Quote Originally Posted by Erayd View Post
    If you've correctly described your problem, it sounds like preg_replace_callback would be a smarter solution. Is there any reason why you can't use that?
    never heard of it

    Mike.
    what are you doing looking in here?

  4. #4
    Gone Erayd's Avatar
    Join Date
    Dec 2004
    Location
    Wellington, NZ
    Posts
    5,761

    Default Re: PHP preg_replace setting variables

    Quote Originally Posted by Mike View Post
    doesn't seem to let me... what am I doing wrong?
    You're forgetting to escape the '$', and as a result the variable substitution is occurring in the string rather than during the execution phase. Either escape it with a backslash, or use single quotes.

    never heard of it
    Take a look here.
    If you are interested in reading fanfiction on a mobile device or ebook reader, please visit flagfic.com.

  5. #5
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,391

    Default Re: PHP preg_replace setting variables

    Quote Originally Posted by Erayd View Post
    You're forgetting to escape the '$', and as a result the variable substitution is occurring in the string rather than during the execution phase. Either escape it with a backslash, or use single quotes.



    Mike.
    what are you doing looking in here?

  6. #6
    Mike Mike's Avatar
    Join Date
    Dec 2004
    Location
    Rosarito, Mexico
    Posts
    1,391

    Default Re: PHP preg_replace setting variables

    Thanks Erayd

    Mike.
    what are you doing looking in here?

  7. #7
    Gone Erayd's Avatar
    Join Date
    Dec 2004
    Location
    Wellington, NZ
    Posts
    5,761

    Default Re: PHP preg_replace setting variables

    Quote Originally Posted by Mike View Post
    Thanks Erayd
    No problem .
    If you are interested in reading fanfiction on a mobile device or ebook reader, please visit flagfic.com.

Similar Threads

  1. php session variables
    By bliss in forum PressF1
    Replies: 2
    Last Post: 16-05-2004, 09:02 PM
  2. draw using variables from spreadsheet
    By felix in forum PressF1
    Replies: 0
    Last Post: 15-01-2004, 01:36 PM
  3. session variables expire
    By sc0ut in forum PressF1
    Replies: 1
    Last Post: 28-11-2003, 01:55 PM
  4. Setting environment variables
    By airdale in forum PressF1
    Replies: 4
    Last Post: 27-10-2003, 08:56 PM
  5. Replies: 1
    Last Post: 22-12-2001, 02:26 PM

Posting Permissions

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