If you're using a delimiter other than "/" then give it as the second parameter
Code: Select all
<?php
/*
Written by d11wtq of http://forums.devnetwork.net/
regex_escape_string(string string [, string delimiter])
Escapes the relevant characters in a string in preparation
-- for including in a regex pattern.
2005-07-18
*/
function regex_escape_string($string, $delimiter='/') {
$nasties = array ( //Stuff to always escape
'.',
'?',
'*',
'+',
'-',
'^',
'$',
'(',
')',
'[',
']',
'{',
'}',
'\\',
'|'
);
$nasties[] = $delimiter; //Whatever delimiter is being used
$goodies = array();
foreach ($nasties as $v) {
$goodies[] = '\\'.$v; //Escaped versions
}
$string = str_replace($nasties, $goodies, $string); //Fix the string
return $string;
}
?>Code: Select all
$input = 'http://www.foo.bar/page.htm#bob';
$pattern = '#\\w+(\\d*?)56 '.regex_escape_string($input, '#').'$#is';