Connecting Tech Pros Worldwide Forums | Help | Site Map

regular expression to replace space by underscore

Newbie
 
Join Date: Dec 2007
Posts: 9
#1: Mar 5 '08
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's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,080
#2: Mar 5 '08

re: regular expression to replace space by underscore


Quote:
It get thrown the following error and can't figure out what it's trying to tell me
Expand|Select|Wrap|Line Numbers
  1. 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.
Newbie
 
Join Date: Mar 2008
Posts: 1
#3: Mar 5 '08

re: regular expression to replace space by underscore


please i want creat a step by step online form, pls kindly give me steps to creat it thanks
Newbie
 
Join Date: Dec 2007
Posts: 9
#4: Mar 5 '08

re: regular expression to replace space by underscore


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

preg_replace("/\s/" , "_" , $url));
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,080
#5: Mar 6 '08

re: regular expression to replace space by underscore


The slashes indicate the beginning and end of an expression not the quotes.
So the 'e' was ouside the expression
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#6: Mar 6 '08

re: regular expression to replace space by underscore


Quote:

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.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,080
#7: Mar 6 '08

re: regular expression to replace space by underscore


And the e-modifier treats the replacement string as php code.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Mar 6 '08

re: regular expression to replace space by underscore


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
Reply


Similar PHP bytes