brady123 Posted May 18, 2011 Share Posted May 18, 2011 For all of you RegEx experts...here is my code: $str=preg_replace("/\[word=(.)(.*)\](.*)\[\/word\]/Usi", "<a href=\"http://www.accountingconcern.com/accounting-dictionary/".strtoupper('\\1')."/\\1\\2/ \">\\3</a>", $str); Basically it is taking [word=debit]Debit[/word] and making it link. The problem is, where I have the first \\1, I need that letter capitalized - not working. I'm not getting an error, it is just not capitalizing that letter. Any ideas? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/ Share on other sites More sharing options...
btherl Posted May 18, 2011 Share Posted May 18, 2011 strtoupper() will be applied to the literal string '\\1' there, not to the result of substituting \1 with the captured text from the expression. If you want to call code on the captured result you can use the "e" flag: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217280 Share on other sites More sharing options...
brady123 Posted May 18, 2011 Author Share Posted May 18, 2011 Hmm, I've read through the manual, but it's a bit confusing, and didn't have any examples. Do I just add the e to my /Usie modifiers? I tried that, and get an error: syntax error, unexpected '<' If you don't mind, would you help enlighten me a little? Thanks! Also, as far as calling the code, the \\1, \\2, and \\3 work just fine (although I'm sure there's an issue with using it inside another php tag, which is the problem I assume)) Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217288 Share on other sites More sharing options...
brady123 Posted May 18, 2011 Author Share Posted May 18, 2011 Here is the example from PHP.net <?php preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body); ?> It seems like that it was I'm looking for, but now I'm getting an error that I have an unexpected < . Are these not allowed with the /e modifier? Can't find that information anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217294 Share on other sites More sharing options...
btherl Posted May 18, 2011 Share Posted May 18, 2011 I don't get any error with the code you pasted. So I think the problem is that the replacement string, after substitution, isn't valid php syntax. Which is a bit problematic. Are you expecting there to be single quotes inside the matched characters in your pattern? If you aren't then there's likely a problem with the pattern, and the error should go away once that's fixed. Also please copy and paste the error from php exactly as is, it helps a lot to find the problem. Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217296 Share on other sites More sharing options...
brady123 Posted May 18, 2011 Author Share Posted May 18, 2011 Ok, with this modification (added the 'e' to my modifiers) $str=preg_replace("/\[word=(.)(.*)\](.*)\[\/word\]/Usie", "<a href=\"http://www.accountingconcern.com/accounting-dictionary/".strtoupper('\\1')."/\\1\\2/\">\\3</a>", $str); I get this error: Parse error: syntax error, unexpected '<' in DIRECTORY/functions.php(23) : regexp code on line 1 Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: <a href="http://www.accountingconcern.com/accounting-dictionary/o/occurrence/">Occurrence</a> in /DIRECTORY/functions.php on line 23 If I remove the 'e' modifier, the URL would look as follows (with no errors...works fine): http://www.accountingconcern.com/accounting-dictionary/o/occurrence/ I'm trying to make it look like: http://www.accountingconcern.com/accounting-dictionary/O/occurrence/ Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217302 Share on other sites More sharing options...
btherl Posted May 18, 2011 Share Posted May 18, 2011 Try this: $str = "[word=foo]bar[/word]"; $str=preg_replace("/\[word=(.)(.*)\](.*)\[\/word\]/Usie", "'<a href=\"http://www.accountingconcern.com/accounting-dictionary/'.strtoupper('\\1').'/\\1\\2/\">\\3</a>'", $str); print "$str\n"; The entire replacement string must be valid php code. Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217305 Share on other sites More sharing options...
brady123 Posted May 18, 2011 Author Share Posted May 18, 2011 You're a genius! All I need were those tick marks? I'll have to go through in-depth to see why the original didn't work (not being valid PHP code). Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217307 Share on other sites More sharing options...
btherl Posted May 19, 2011 Share Posted May 19, 2011 Yep it's just the tick marks. To work out what's going on you need to think like the php parser - the first thing it will do is see the double quoted string and process all the backslashes. The result of that will be like this: '<a href="http://www.accountingconcern.com/accounting-dictionary/'.strtoupper('\1').'/\1\2/">\3</a>' Then php will replace the \1, \2 and \3 with the exact text it found from the expression, giving: '<a href="http://www.accountingconcern.com/accounting-dictionary/'.strtoupper('f').'/foo/">bar</a>' This final result must be valid php code, and it is because it's a quoted string joined to strtoupper('f') joind to another quoted string. Quote Link to comment https://forums.phpfreaks.com/topic/236798-strtoupper-inside-preg_replace-not-working/#findComment-1217318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.