0

Let's say I have these texts in my Wordpress title, post content and comments:

#TSLA

$TSLA

What I want to do is for Wordpress to identify the text #TSLA and $TSLA and automatically replace with a hyperlink, like this:

<a href="https://www.mywebsite.com/?s=TSLA">#TSLA</a>
<a href="https://www.mywebsite.com/?s=TSLA">$TSLA</a>

As for $, I just want to add a hyperlink for alphabet (A to Z), not with numbers.

Basically, very similar to what Twitter does.

Is there any function.php code to do that?

Thank you.

EDIT: Ok so, I think I'm getting somewhere thanks to Markus Zeller.

Basically here's what I have got so far:

<?php
$x = "#TSLA";
$z = preg_replace("/^#*/", "", $x);
echo "<a href='?s=$z'>#$z</a>";
?>

which is echoing exactly what I need.

Finding the line that begins with # and replaces with a hyperlink.

I still have two things to figured out:

  1. How to do the same with $ instead of #.
  2. It's now echoeing, I need it to be globally replaced on wordpress by putting a line in functions.php

Any further help would be appreciated.

robert0
  • 435
  • 2
  • 12
  • Yes, you can achieve this with a [regular expression](https://www.php.net/manual/en/function.preg-replace). – Markus Zeller Mar 06 '21 at 20:51
  • Thanks Markus, I think I'm on the right track, could you please help me further. I have edited the post. thanks. – robert0 Mar 06 '21 at 23:23
  • I know it can be hard to come up with the right terms, but simply searching for "php replace word with link" turns up many hits here on SO, all with code which solves your problem. Pls try searching before posting a new question, or if you found those questions already, pls describe how you tried them and why they didn't work. https://stackoverflow.com/questions/33460311/automatic-convert-word-to-link-in-php, https://stackoverflow.com/questions/380773/php-regular-expression-to-replace-word-with-link, https://stackoverflow.com/questions/5889873/scan-a-string-and-replace-tags-with-links – Don't Panic Mar 07 '21 at 09:27

1 Answers1

2

Using a regular expression you can find and replace text by a pattern.

You changed your question, so this answer fits your original question. But with this hint, it should be easy for you to adapt.

We are searching for a # or a $ followed by word characters and replace it with a html URL.

$text = <<<'__TEXT'
Hello, my friend. A $tesla is a nice car.
Hello, my friend. A #tesla is a nice car.
__TEXT;

$new = preg_replace('/(\$|#)(\w+)/s', '<a href="https://www.example.com/?s=\2">\1\2</a>', $text);

$new contains:

Hello, my friend. A <a href="https://www.example.com/?s=tesla">$tesla</a> is a nice car.
Hello, my friend. A <a href="https://www.example.com/?s=tesla">#tesla</a> is a nice car.
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • Thanks Markus, that's exactly what I need. Sorry, i'm new to php. The last question, what would be the final code to add to functions.php, so that this would globally affect the whole content that's already on my wordpress blog. Thank you for your effort. – robert0 Mar 07 '21 at 10:53
  • 1
    I am no wordpress expert, so I suppose it depends on where you want to filter the text. But I think [add_filter()](https://developer.wordpress.org/reference/functions/add_filter/) is what you are searching for. – Markus Zeller Mar 07 '21 at 10:58
  • I have managed to add_ filter() for content. It does the job great, but it also have some conflicts. It replaces ' and " with a weird ’ and links to #8217. Not sure why it's trying to replace those two characters ' and " even if they are not set for replacement. Any ides? – robert0 Mar 07 '21 at 11:38
  • 1
    Looks like html escaping, but as stated I am no wordpress expert. Check for other filters or maybe order matters. If that does not help, search for that or ask a new question. – Markus Zeller Mar 07 '21 at 11:40
  • No problem, thanks for your help and effort, you helped a lot, I'll keep looking. – robert0 Mar 07 '21 at 11:43
  • Maybe this [question](https://stackoverflow.com/questions/39810919/my-special-characters-display-as-8217-in-wordpress) helps you. – Markus Zeller Mar 07 '21 at 11:48
  • I think I'm almost already there. I have updated my question. Would appreciate if you could look at it. thanks. – robert0 Mar 07 '21 at 13:48
  • Please don't change questions after they are answered (please revert). Just create a new question, because it is a different problem! – Markus Zeller Mar 07 '21 at 14:07