Fedora Linux Support Community & Resources Center
  #1  
Old 16th July 2007, 08:31 PM
CD-RW's Avatar
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 555
Matching the '\' pattern in PHP preg_replace()

Is it possible to match the '\' backslash RE escape character in a search pattern please? I have tried my best to find a way to match on this character, but no luck so far.

PHP Code:
php $var 'Now with the 50% discount it $offers the \best prices';

php > echo preg_replace('-\[\]\-''X'$var);
PHP Warning:  preg_replace(): No ending delimiter '-' found in php shell code on line 1

php 
> echo preg_replace('/\[\]\/''X'$var);
PHP Warning:  preg_replace(): No ending delimiter '/' found in php shell code on line 1

php 
> echo preg_replace('-\-''X'$var);
PHP Warning:  preg_replace(): No ending delimiter '-' found in php shell code on line 1

php 
> echo preg_replace('-\\-''X'$var);
PHP Warning:  preg_replace(): No ending delimiter '-' found in php shell code on line 1

php 
> echo preg_replace('-/\-''X'$var);
PHP Warning:  preg_replace(): No ending delimiter '-' found in php shell code on line 1 
Any help gratefully appreciated.

TIA
Reply With Quote
  #2  
Old 16th July 2007, 11:40 PM
pigpen's Avatar
pigpen Offline
Registered User
 
Join Date: Nov 2003
Location: Regensburg, Germany
Age: 42
Posts: 447
PHP Code:
php > echo preg_replace('/\\\\/''X'$var);
Now with the 50discount it $offers the Xbest prices

php 
> echo preg_replace('/\\\/''X'$var);
Now with the 50discount it $offers the Xbest prices 
had my own fight with the backslash before ;-)
__________________
/(bb|[^b]{2})/ -- that is the question!

Last edited by pigpen; 17th July 2007 at 01:10 AM.
Reply With Quote
  #3  
Old 17th July 2007, 03:55 PM
CD-RW's Avatar
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 555
Thumbs up

Thankyou pigpen. After all that I found the following oneliner that does it all, it even deals with the back and forward slashes as well.

preg_quote
(PHP 3 >= 3.0.9, PHP 4, PHP 5)

preg_quote -- Quote regular expression characters

Description

string preg_quote ( string str [, string delimiter] )

preg_quote() takes str and puts a backslash in front of every character that is part of the regular expression syntax. This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.

The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | :

PHP Code:
<?php
$keywords 
'$40 for a g3/400';
$keywords preg_quote($keywords'/');
echo 
$keywords// returns \$40 for a g3\/400
?>
Thankyou for your help anyway.
Reply With Quote
  #4  
Old 18th July 2007, 12:01 AM
pigpen's Avatar
pigpen Offline
Registered User
 
Join Date: Nov 2003
Location: Regensburg, Germany
Age: 42
Posts: 447
Wow! I didn't even know this function! Thank YOU, CD-RW!!
__________________
/(bb|[^b]{2})/ -- that is the question!
Reply With Quote
Reply

Tags
matching, pattern, php, pregreplace

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl pattern matching ibbo Programming & Packaging 0 30th April 2009 11:03 AM
Pattern Matching petervoldon Using Fedora 3 17th October 2008 10:19 AM
target pattern contains no `%' (firefox) CopyrightPhilly Using Fedora 1 8th April 2008 07:17 AM
DVD player, criss-cross pattern tashirosgt Wibble 1 18th July 2007 07:06 PM


Current GMT-time: 12:48 (Saturday, 25-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat