Jump to content

preg_replace help


marcus

Recommended Posts

I'm trying to make it so:

 

[profile=username] replaces with username(username)

 

username() is a function I have that links the "username" to that profile

 

$string = "[profile=marcus;]";

echo "Before: $string<br>\n";
echo "After: ";
echo preg_replace("[profile=(.*?);]",username("\\1"),$string);

 

I get:

 

 

Before: [profile=marcus;]

After: [marcus]

 

 

The after statement is producing a link to that profile with an image. The image identifies the user's level, the link is correct.

 

How would I remove the brackets on the start and the end of "[marcus]" ?

Link to comment
https://forums.phpfreaks.com/topic/60625-preg_replace-help/
Share on other sites

I don't want the braces, and the variable inside the username function is not passing correctly.

 

Username function does: [puts image on administrative level][link to profile]

 

I'm getting:

 

[frozenpic][link to profile]

 

Therefore the username isn't in existence.

Link to comment
https://forums.phpfreaks.com/topic/60625-preg_replace-help/#findComment-301900
Share on other sites

You have two problems:

 

1. PREG requires delimiters.

2. [ and ] are metacharacters.

 

The [ and ] around your pattern are being considered delimiters and therefore not actually matching a pair of braces. You need to add a delimiter, then escape the braces in order to match them:

 

/\[profile=(.*?);\]/

Link to comment
https://forums.phpfreaks.com/topic/60625-preg_replace-help/#findComment-302328
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.