backlash in regex and php?...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

backlash in regex and php?...

Post by sebnewyork »

Hi
I am trying to use a regular expression, but it seems that php doesn't like the backlashes in the expression, because it returns:
"Delimiter must not be alphanumeric or backslash in...bla bla bla".

the regular expression appreas in the code:

Code: Select all

<?php
$pattern ="include\('^.*?$'\)";
?>
include\('^.*?$'\)

beeing the expression (i am new to regex, so I wouldn't be surprised if it doesn't make any sense).
Just so you know, with this expression I'm trying to match anything between
"include('"
and
"')"

However, I should first be able to use backlashes as needed in my regex. How do I do that without upseting php parsing?
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post by skehoe »

Hey,

You don't need the backslashes... Here's an example:

Code: Select all

<?php
$string = 'include("/foo/bar/bif/baz.php")';
$pattern = '/include(.*)/';

if (preg_match($pattern, $string, $matches))
{
    print_r($matches);
}
else
{
    echo "Match Failed\n\n";
}

?>
~Scott
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

sebnewyork-
wheres your delimiters?

you dont have any. you MUST use delimiters w/ pcre

do like this

Code: Select all

$pattern ="/include\('^.*?$'\)/";

btw- that regex pattern isnt going to work. it needs to be something like this

Code: Select all

$pattern ="/include\s*\('([^']+)'\)/";
Post Reply