0

I want to make mentions similar to Twitter but for the user profiles i use ID not userNames so i creat a function that get the UserID based on userName and in a procedural code is working but in oop dosent ..

$title = ' Hey @Stackoverflow can u help me ?';

$mentions = (preg_replace("/\@([a-zA-Z0-9\-_]{3,30})/e", 
    "'<a href=\"http://mySite.com/user:'.$this->mentionName('$1').
'\">@$1</a>'",$title));

expected url result

http://mySite.com/user:1

not expected result

http://mySite.com/user:Stackoverflow

// This is a loop not a single string

3

2 Answers 2

1

You can't do that.

Look into preg_replace_callback function, or assign your match to another variable, and use that for your class function.

Sign up to request clarification or add additional context in comments.

2 Comments

OK but the $mentions and $title are in __construct and function mentionName is outSide..
Then define the callback function as an array ($class, $method)... and if outSide isn't in scope then you need to make it in scope
0

If you want to use preg_replace in combination with a function that evaluates the result and modifies it, use preg_replace_callback

http://php.net/manual/en/function.preg-replace-callback.php

function returnUser($matches){
    return $this->mentionName($matches[1]);
}

$mentions = preg_replace_callback("/\@([a-zA-Z0-9\-_]{3,30})/e", "returnUser",$title));

4 Comments

Try again. Forgot to add _callback
Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'returnUser', to be a valid callback in
. Btw in function mentionName is a query that pull the data from database
return $this->mentionName($matches[1]); won't work from that function. The preg_replace_callback should work, otherwise check the documentation and do some testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.