Connecting Tech Pros Worldwide Forums | Help | Site Map

How to escape string for preg_match?

squash@peoriadesignweb.com
Guest
 
Posts: n/a
#1: May 10 '06
I have a string equal to 'www/' that I want to use in a preg_match. Php
keeps giving me the warning:

Warning: preg_match(): Unknown modifier '/'

How can I escape the string so the / in www/ is not interpreted in the
preg_match ?

tim
Guest
 
Posts: n/a
#2: May 10 '06

re: How to escape string for preg_match?



squash@peoriadesignweb.com wrote:[color=blue]
> I have a string equal to 'www/' that I want to use in a preg_match. Php
> keeps giving me the warning:
>
> Warning: preg_match(): Unknown modifier '/'
>
> How can I escape the string so the / in www/ is not interpreted in the
> preg_match ?[/color]

Either with \
preg_match( '/www\//', $str )

or use a diffrent charcter as a delimiter
preg_match( '|www/|', $str )

Tim

John Dunlop
Guest
 
Posts: n/a
#3: May 10 '06

re: How to escape string for preg_match?


tim:
[color=blue]
> Either with \
> preg_match( '/www\//', $str )[/color]

or preg_quote()
http://www.php.net/manual/en/function.preg-quote.php
[color=blue]
> or use a diffrent charcter as a delimiter
> preg_match( '|www/|', $str )[/color]

--
Jock

squash@peoriadesignweb.com
Guest
 
Posts: n/a
#4: May 10 '06

re: How to escape string for preg_match?



tim wrote:
[color=blue]
> Either with \
> preg_match( '/www\//', $str )
>
> or use a diffrent charcter as a delimiter
> preg_match( '|www/|', $str )
>[/color]

Actually I want to use $str in the preg_match, i.e:

preg_match( "$str" , 'www' )

but since $str is 'www/', the forward slashes causes a problem

Tim Van Wassenhove
Guest
 
Posts: n/a
#5: May 11 '06

re: How to escape string for preg_match?


On 2006-05-10, squash@peoriadesignweb.com <squash@peoriadesignweb.com> wrote:[color=blue]
>
> tim wrote:
>[color=green]
>> Either with \
>> preg_match( '/www\//', $str )
>>
>> or use a diffrent charcter as a delimiter
>> preg_match( '|www/|', $str )
>>[/color]
>
> Actually I want to use $str in the preg_match, i.e:
>
> preg_match( "$str" , 'www' )
>
> but since $str is 'www/', the forward slashes causes a problem[/color]

As already said: they are only a problem if you use them as delimiter...
Since webdevelopment is usually about URLs (with forward slashes) i
agree that is better to use a delimiter like | instead of /.


--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Closed Thread