Jump to content


Photo

preg quote problem


  • Please log in to reply
2 replies to this topic

#1 fantomel

fantomel

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts

Posted 25 January 2013 - 09:19 AM

Hello i have a problem with the following code:

$uri = '/index.php/';
$this->finalRegex = '/^' . preg_quote($uri, '/') . '$/';

if(preg_match($this->finalRegex, '/index.php')
{
echo "works!";
}


It returns
string(18) "/^\/index\.php\/$/"
As you can see it adds a \ before .php which would be inconsistent with the string i'm trying to match ( offered an example). can you help me understand how to fix i tried everything it came in my mind.

#2 fantomel

fantomel

    Advanced Member

  • Members
  • PipPipPip
  • 186 posts

Posted 26 January 2013 - 05:16 AM

bump please anyone ? :D

#3 .josh

.josh

    Grumpy Old Man

  • Administrators
  • 13,824 posts
  • LocationWhere You Live.

Posted 26 January 2013 - 09:13 AM

preg_quote escapes all characters in your string that mean something special to the regex engine. The dot is escaped because it is a meta character that matches (almost) any one character. Since you want to match for a literal dot instead of any character, it is escaped. So in other words, it is working as intended.

p.s. - If you had read the manual entry for preg_quote, you would have seen this right there at the top. In case you didn't know, php.net has a nifty shortcut to their functions, just go to www.php.net/functionNameHere

Posted Image
Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com