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
Share on other sites

I'm confused at what you're trying to achieve on a step-by-step basis. Are you saying everything works fine, except the end result needs to remove the braces? Or are the braces the result of something not being processed correctly?

Link to comment
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
Share on other sites

I'm still getting the wrong image. I'm trying to get image "6", but I keep getting "4"

 

I also believe that the variable is NOT passing threw the username function.

 

I've tried returning the username value inside the function but it's blank.

Link to comment
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
Share on other sites

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.