0

I got a Button, that creates this template code

[5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)

The filename and file size vary und the user can change the ##LINKNAME##, as well. This code goes to a database and when I get it back, I want to replace it to

<a href="5afc4076e9f1b1526481014.pdf" target="_blank">##LINKNAME## <i>(96.51 kb)</i></a>

I think I need to use preg_replace() but I am not really good at regular expressions. I stopped here:

<?php
$string = ' [5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)';
$regex = '[[a-zA-Z0-9]+.pdf](.*?)\s';
$replace = 'I DONT KNOW';
echo preg_replace($regex, $replace, $string);
?>

I know that this is a complete mess, but I'm not getting any results as long as I don't know the regex and the correct $replace.

1
  • does the ##LINKNAME## contain space? what characters are allowed in the LinkName? Commented May 16, 2018 at 15:19

1 Answer 1

2

Regex: ^\[([^\]]+)\](\w+)\s\(([^)]+)\)$

Replace with: <a href="\1" target="_blank">\2<i>(\3)</i></a>

Demo

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

1 Comment

Thank you! This is working perfectly. I'll take some time, understanding what you put there. Thank you for the Demo. This is really good to learn and understand!

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.