str_ireplace() for php4?

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

    str_ireplace() for php4?

    Hi,

    would anybody have a version of str_irepalce(),
    (http://uk.php.net/manual/en/function.str-ireplace.php) for php4?

    The doc mentions eregi_replace() and preg_repalce() but I cannot get my head
    around it.

    Would anybody have a few lines of code that would reproduce the
    functionality above.

    What I actually need to do is a search an replace of a web address.

    Simon


  • Janwillem Borleffs

    #2
    Re: str_ireplace() for php4?

    Simon wrote:[color=blue]
    > would anybody have a version of str_irepalce(),
    > (http://uk.php.net/manual/en/function.str-ireplace.php) for php4?
    >
    > The doc mentions eregi_replace() and preg_repalce() but I cannot get
    > my head around it.
    >[/color]

    if (!function_exis ts('str_ireplac e')) {
    function str_ireplace($n eedle, $str, $haystack) {
    return preg_replace("/$needle/i", $str, $haystack);
    }
    }


    JW


    Comment

    • Simon

      #3
      Re: str_ireplace() for php4?

      > Simon wrote:[color=blue][color=green]
      >> would anybody have a version of str_irepalce(),
      >> (http://uk.php.net/manual/en/function.str-ireplace.php) for php4?
      >>
      >> The doc mentions eregi_replace() and preg_repalce() but I cannot get
      >> my head around it.
      >>[/color]
      >
      > if (!function_exis ts('str_ireplac e')) {
      > function str_ireplace($n eedle, $str, $haystack) {
      > return preg_replace("/$needle/i", $str, $haystack);
      > }
      > }
      >
      >[/color]

      Thanks!

      Just out of curiosity, would it work if $needle, $str, $haystack had
      escape/special characters?

      Simon


      Comment

      • Janwillem Borleffs

        #4
        Re: str_ireplace() for php4?

        Simon wrote:[color=blue]
        > Just out of curiosity, would it work if $needle, $str, $haystack had
        > escape/special characters?
        >[/color]

        Good point. You need to escape reserved regular expression characters in
        $needle as follows:

        if (!function_exis ts('str_ireplac e')) {
        function str_ireplace($n eedle, $str, $haystack) {
        $needle = preg_quote($nee dle, '/');
        return preg_replace("/$needle/i", $str, $haystack);
        }
        }

        $str and $haystack do not need to be modified.


        JW


        Comment

        • Chung Leong

          #5
          Re: str_ireplace() for php4?


          Simon wrote:[color=blue]
          > Hi,
          >
          > would anybody have a version of str_irepalce(),
          > (http://uk.php.net/manual/en/function.str-ireplace.php) for php4?
          >
          > The doc mentions eregi_replace() and preg_repalce() but I cannot get my head
          > around it.
          >
          > Would anybody have a few lines of code that would reproduce the
          > functionality above.
          >
          > What I actually need to do is a search an replace of a web address.
          >
          > Simon[/color]

          Just grab the function from PEAR::PHP_Compa t
          (http://pear.php.net/package/PHP_Compat/).

          Comment

          • Toby Inkster

            #6
            Re: str_ireplace() for php4?

            Janwillem Borleffs wrote:
            [color=blue]
            > Simon wrote:[color=green]
            >> Just out of curiosity, would it work if $needle, $str, $haystack had
            >> escape/special characters?[/color]
            >
            > Good point. You need to escape reserved regular expression characters in
            > $needle as follows:
            >
            > if (!function_exis ts('str_ireplac e')) {
            > function str_ireplace($n eedle, $str, $haystack) {
            > $needle = preg_quote($nee dle, '/');
            > return preg_replace("/$needle/i", $str, $haystack);
            > }
            > }
            >
            > $str and $haystack do not need to be modified.[/color]

            What if $str contained "\1"?

            --
            Toby A Inkster BSc (Hons) ARCS
            Contact Me ~ http://tobyinkster.co.uk/contact

            Comment

            • Janwillem Borleffs

              #7
              Re: str_ireplace() for php4?

              Toby Inkster wrote:[color=blue]
              > What if $str contained "\1"?
              >[/color]

              You probably mean "\\1", which will contain pattern matches grouped by
              parenthesis, but these will be ignored as preg_quote is used to escape the
              parenthesis when occurring in $needle.


              JW


              Comment

              • Chung Leong

                #8
                Re: str_ireplace() for php4?


                Janwillem Borleffs wrote:[color=blue]
                > Toby Inkster wrote:[color=green]
                > > What if $str contained "\1"?
                > >[/color]
                >
                > You probably mean "\\1", which will contain pattern matches grouped by
                > parenthesis, but these will be ignored as preg_quote is used to escape the
                > parenthesis when occurring in $needle.[/color]

                Actually, \1 would end up being an empty string if the pattern doesn't
                specify capturing.

                Comment

                • Simon

                  #9
                  Re: str_ireplace() for php4?

                  >>[color=blue][color=green]
                  >> Simon[/color]
                  >
                  > Just grab the function from PEAR::PHP_Compa t
                  > (http://pear.php.net/package/PHP_Compat/).
                  >[/color]

                  That's great, didn't know it existed.

                  Thanks for that.

                  Simon


                  Comment

                  Working...