regular expression to replace space by underscore

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • a2rodger
    New Member
    • Dec 2007
    • 9

    regular expression to replace space by underscore

    I am using the following code to replace spaces with "_" for my URL rewriting
    function.

    $url = urlencode(preg_ replace("/\s/e" , "_" , $url));


    It get thrown the following error and can't figure out what it's trying to tell me,

    Notice: Use of undefined constant _ - assumed '_' in : regexp code on line 1

    Help!

    (Note: I'm using PHP Version 5.2.4)
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    It get thrown the following error and can't figure out what it's trying to tell me
    Code:
    Notice: Use of undefined constant _ - assumed '_' in : regexp code on line 1
    When php finds characters outside quotes it knows it is useable code.
    A lone word with no dollar $ in front is assumed to be a constant.
    Constants should be defined before use or an error is generated
    This means you have an underscore outside the quotes somewhere.
    [PHP]preg_replace("/\s/e" , "_" , $url));[/PHP]
    Use single quotes to avoid confusion but it may be in $url.

    Comment

    • ericoos
      New Member
      • Mar 2008
      • 1

      #3
      please i want creat a step by step online form, pls kindly give me steps to creat it thanks

      Comment

      • a2rodger
        New Member
        • Dec 2007
        • 9

        #4
        Getting rid of the 'e' seemed to solve the problem.
        Although I'm not sure why.

        preg_replace("/\s/" , "_" , $url));

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          The slashes indicate the beginning and end of an expression not the quotes.
          So the 'e' was ouside the expression

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by code green
            The slashes indicate the beginning and end of an expression not the quotes.
            So the 'e' was ouside the expression
            As was intended; the 'e' is a modifier and, therefore, comes after the expression.

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              And the e-modifier treats the replacement string as php code.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                A new thread has been split off this one. See want to lear regular expressions

                Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

                This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

                MODERATOR
                Last edited by ronverdonk; Mar 6 '08, 10:31 AM. Reason: typo

                Comment

                Working...