Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
in Q2A Core by

I'm using this to replace $content if it contains @ sign at the beginning of the string. The code works outside Q2A.

$content = preg_replace("/^@([A-Za-z0-9_]*)/", "<a href=\"/user/$1\">@$1</a>", $content);

echo $content;

However, when using it in Q2A, nothing happens, no error reported, but the string $content is not replaced.

Example of a string:

$content = "@JoeBiden Hello boy! I'm a @tester.";

Funny enough, if I remove the ^ sign, the code works in Q2A, but it changes my purpose.

My PHP Version: 7.2

Can't reproduce with PHP 7.0 (Q2A 1.8.5).
If I simply create a PHP file within Q2A folder and test the code, the code is fine. But if I put it in a plugin (an override plugin to be more specific), it won't work as expected. (Q2A 1.8.4). I was wondering if there is a hidden code beautifier feature that rearrange codes.

I do notice that if I write a code like this <a rel="nofollow" href="https://sdsdd...."> ... then it will be converted into <a href="https://sdsds" rel="nofollow">....

I'm skeptical that such feature may interfere negatively in some cases.

I was the one who asked this question, too.


1 Answer

+2 votes
What exactly do you mean by "using it in Q2A"? Did you check the exact value of the $content variable before using the regex? If it works outside Q2A then I'd bet that the variable is something different depending on where you used it.

For example maybe it has HTML around it like "<p>@JoeBiden Hello boy! I'm a @tester.</p>"
Well, maybe because WYSIWYG editor often adds <p> tags. Thanks.
You are correct. CKEditor added <p> tags.