470,817 Members | 1,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,817 developers. It's quick & easy.

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)
Mar 5 '08 #1
7 12849
code green
1,726 Expert 1GB
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.
Mar 5 '08 #2
please i want creat a step by step online form, pls kindly give me steps to creat it thanks
Mar 5 '08 #3
Getting rid of the 'e' seemed to solve the problem.
Although I'm not sure why.

preg_replace("/\s/" , "_" , $url));
Mar 5 '08 #4
code green
1,726 Expert 1GB
The slashes indicate the beginning and end of an expression not the quotes.
So the 'e' was ouside the expression
Mar 6 '08 #5
Markus
6,050 Expert 4TB
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.
Mar 6 '08 #6
code green
1,726 Expert 1GB
And the e-modifier treats the replacement string as php code.
Mar 6 '08 #7
ronverdonk
4,258 Expert 4TB
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
Mar 6 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Neri | last post: by
3 posts views Thread by James D. Marshall | last post: by
1 post views Thread by Don | last post: by
8 posts views Thread by P K | last post: by
7 posts views Thread by Billa | last post: by
18 posts views Thread by Lit | last post: by
1 post views Thread by NvrBst | last post: by
1 post views Thread by AAaron123 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.