Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is easiest way to convert backticks into '<code>' and '</code>'?

Tags:

regex

php

What is the easiest way to make this PHP function so that every other backtick becomes a beginning and ending HTML tag, so that:

'in for loops, use `$index` for 0-n values and `$count` for 1-n values'

becomes:

'in for loops, use <code>$index</code> for 0-n values and <code>$count</code> for 1-n values'

e.g.

$line = 'in for loops, use `$index` for 0-n values and `$count` for 1-n values';
echo getFormattedLine($line);

function getFormattedLine($line) {
    return $line; //...str_replace, regular expressions, etc.
}
like image 393
Edward Tanguay Avatar asked Dec 27 '22 12:12

Edward Tanguay


1 Answers

Seems pretty trivial:

preg_replace('/`(.*?)`/', '<code>$1</code>', $str);
like image 167
Lightness Races in Orbit Avatar answered Mar 17 '23 07:03

Lightness Races in Orbit