0

Hello everyone what I want to achieve with php is to find a value in between two words (for all occurances) and replace this with another string, then remove the words around the value. Here's an example

$mysting = "[link]http://website.com[/link] bla bla bla bla [link]http://google.com[/link]";

What I want to achieve is make this string:

$newString = "<a href='http://website.com'>http://website.com</a> bla bla bla bla <a href='http://google.com'>http://google.com</a>";

How can I do this? This is what I have for now

preg_match("/(?<=[link])(.*)(?=[/link])/", $formData ,$match);

Thanks in advance!!

2

1 Answer 1

1

Use preg_replace function:

$mysting = "[link]http://website.com[/link] bla bla bla bla [link]http://google.com[/link]";
$new_str = preg_replace("/\[link\]([^\[]+)\[\/link\]/", "<a href='$1'>$1</a>", $mysting);

print_r($new_str);

The output(as source code):

<a href='http://website.com'>http://website.com</a> bla bla bla bla <a href='http://google.com'>http://google.com</a>
Sign up to request clarification or add additional context in comments.

Comments

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.