preg_replace and literal $

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Justin Koivisto

    preg_replace and literal $

    I am replacing a string in a text block that has a literal $ in it, and
    preg_replace is seeing it as a backreference. Here is what I am using:

    foreach($price_ lists as $list)
    $x=preg_replace ('/--PRICE-LIST--/',$list,$x,1);

    OK, so what this does it is takes each array element and replaces only
    the first occurrance of "--PRICE-LIST--" with it. I would have used
    str_replace, but I didn't think it should be necessary to create an
    array with the same number of elements as $price_list just for a simple
    thing like this.

    I did, hoever try:
    $x=str_replace( '--PRICE-LIST--',$price_lists, $x);

    Since the manual page didn't say anything about that case to no avail.

    So right now I have the following:
    foreach($price_ lists as $list)
    $x=preg_replace ('/--PRICE-LIST--/',str_replace(' $','\$',$list), $x,1);

    Which compensates for this. However, that is just one case, I cane
    forsee others showing up. Does anyone know of a way to make preg_replace
    behave as I intend it to? Or does someone have a function for regex-safe
    string encoding?

    TIA!

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

  • Steven Vasilogianis

    #2
    Re: preg_replace and literal $

    Justin Koivisto <spam@koivi.com > writes:
    [color=blue]
    > So right now I have the following:
    > foreach($price_ lists as $list)
    > $x=preg_replace ('/--PRICE-LIST--/',str_replace(' $','\$',$list), $x,1);
    >
    > Which compensates for this. However, that is just one case, I cane forsee
    > others showing up. Does anyone know of a way to make preg_replace behave as
    > I intend it to? Or does someone have a function for regex-safe string
    > encoding?[/color]

    Change it to:
    foreach($price_ lists as $list)
    $x=preg_replace ('/--PRICE-LIST--/', preg_quote($lis t), $x, 1);

    And of course, see <http://us2.php.net/preg_quote>

    HTH,
    --
    steven vasilogianis

    Comment

    • Justin Koivisto

      #3
      Re: preg_replace and literal $

      Steven Vasilogianis wrote:
      [color=blue]
      > Justin Koivisto <spam@koivi.com > writes:
      >
      >[color=green]
      >>So right now I have the following:
      >>foreach($pric e_lists as $list)
      >> $x=preg_replace ('/--PRICE-LIST--/',str_replace(' $','\$',$list), $x,1);
      >>
      >>Which compensates for this. However, that is just one case, I cane forsee
      >>others showing up. Does anyone know of a way to make preg_replace behave as
      >>I intend it to? Or does someone have a function for regex-safe string
      >>encoding?[/color]
      >
      >
      > Change it to:
      > foreach($price_ lists as $list)
      > $x=preg_replace ('/--PRICE-LIST--/', preg_quote($lis t), $x, 1);[/color]

      Thanks! I'm surprized that I didn't see that one.

      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.

      Comment

      • Justin Koivisto

        #4
        Re: preg_replace and literal $

        Steven Vasilogianis wrote:
        [color=blue]
        > Justin Koivisto <spam@koivi.com > writes:
        >
        >[color=green]
        >>So right now I have the following:
        >>foreach($pric e_lists as $list)
        >> $x=preg_replace ('/--PRICE-LIST--/',str_replace(' $','\$',$list), $x,1);
        >>
        >>Which compensates for this. However, that is just one case, I cane forsee
        >>others showing up. Does anyone know of a way to make preg_replace behave as
        >>I intend it to? Or does someone have a function for regex-safe string
        >>encoding?[/color]
        >
        > Change it to:
        > foreach($price_ lists as $list)
        > $x=preg_replace ('/--PRICE-LIST--/', preg_quote($lis t), $x, 1);
        >
        > And of course, see <http://us2.php.net/preg_quote>[/color]

        Unfortunately, that didn't work for my application because there is HTML
        in it, and it escaped all the < and >. However, After reviewing the
        manual page, it looks like I should be OK with only escaping the $ in
        the strings.

        --
        Justin Koivisto - spam@koivi.com
        PHP POSTERS: Please use comp.lang.php for PHP related questions,
        alt.php* groups are not recommended.

        Comment

        Working...