mengo Posted July 11, 2013 Share Posted July 11, 2013 hello,you all. there is a problem let me not understand.i have a few codes like this: $pattern = "/b©(d)\2efgh/"; $subject = "abcddefghijk"; preg_match_all($pattern, $subject, $matches); echo "<pre>"; print_r($matches); echo "</pre>"; i want to use var $pattern capture str "bcddefgh" in var $subject with "\2" back reference,but the result is not what i want,it ouputs nothing but an empty array. i think i did nothing wrong, in php manual, it says a backslash followed by a non-0 number can represent a back reference.but why it fails? is there something wrong with my codes or an error in php manual? is there some other problems in php.ini or somewhere else? help, thanks!! Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/ Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 What is this - © in your pattern ? From what i have understood you try write {1,2}\1 instead of \2 Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440339 Share on other sites More sharing options...
mengo Posted July 11, 2013 Author Share Posted July 11, 2013 value of $pattern is not important ,it can also be replaced by "/(b)c(d)\2efgh/",i don't know why an error occurs when i try to edit the first subpattern,maybe this value is better. why "\2" can't reference the second subpattern "(d)"? thanks! Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440340 Share on other sites More sharing options...
requinix Posted July 11, 2013 Share Posted July 11, 2013 "\2" == chr(2) Escape the \ or use single quotes. Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440351 Share on other sites More sharing options...
mengo Posted July 11, 2013 Author Share Posted July 11, 2013 thank you for your help! I found another weird problem,for example: $pattern = "/(1)(2)(3)(4)(5)(6)(7)((9)\3fgh/"; $subject = "1234567893fghijk"; preg_match_all($pattern, $subject, $matches); echo "<pre>"; print_r($matches); echo "</pre>"; echo chr(2); form \1 to \7, no matter whick back reference is used, there will always get not correct result! But when changed to \8 or \9, if there is a respondding matching substring in variable $subject,while it will get correct result. Where there is a difference between these two conditions? Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440360 Share on other sites More sharing options...
requinix Posted July 11, 2013 Share Posted July 11, 2013 "\3" == chr(3) Are you seeing the pattern? [edit] "\nnn" for n=0-7 is the escape for a character in octal. Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440371 Share on other sites More sharing options...
mengo Posted July 11, 2013 Author Share Posted July 11, 2013 now it is clear! really thanks! may be i should spend some time studying pattern more carefully! Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440373 Share on other sites More sharing options...
requinix Posted July 11, 2013 Share Posted July 11, 2013 Strings, actually. http://www.php.net/manual/en/language.types.string.php Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440374 Share on other sites More sharing options...
mengo Posted July 11, 2013 Author Share Posted July 11, 2013 yes,thanks for your advice! both of them! Link to comment https://forums.phpfreaks.com/topic/280064-help-pcre-subpattern/#findComment-1440376 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.