Jump to content

Recommended Posts

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!

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

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))

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.

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.

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!

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.