1

I will illustrate code what I'm doing:

I have data that has strings such as:

[[-55-]], [[-75-]]

Which I use to do something like:

$var = 'hi my name is [[-55-]] and I think yoda looks like a bald green [[-75-]].';
$var = str_replace('[[-', '<img src="/', $var);
$var = str_replace('-]], '">', $var);

What I would like to do is end up with the image output something like:

<a href="/stuff/55/"><img src="/55.png"></a>

The problem is, that takes more than a simple str_replace and I am beyond terrible at regex.

Any help would be appreciated.

1
  • Are all separated by the same delimeter? If so, read the "[[-55-]], [[-75-]]" into a $string then use, explode(",", $string) and use the $array[55], $array[75] as you need. Commented Jan 17, 2014 at 3:54

1 Answer 1

1

Something like:

$var = 'hi my name is [[-55-]] and I think yoda looks like a bald green [[-75-]].';
$result = preg_replace('/\[\[-(\d+)-\]\]/', '<a href="/stuff/\1/"><img src="/\1.png"></a>', $var);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for writing my answer for me. ;) Only difference is that the RegEx should be: /\[\[-(\d+?)-\]\]/.

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.