Regular Expression Shortcodes

Often on sites, it becomes necessary to insert dynamic information blocks into the texts of pages - banners, phones, etc. As a rule, the content of these blocks changes frequently, which significantly complicates the work of the content manager.

Shortcodes will help simplify this task. Their meaning is that in the HTML-code of the page, instead of the required content, a symbolic code is inserted, for example [[phone]], and when displayed on the front of the site, it is replaced with the necessary information.

Shortcode in visual editor

Double square brackets are used to avoid false substitution.

A simple example of replacing shortcodes with content:

$text = '
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
	[[phone]]
	<p>Sed porta justo sed nibh elementum condimentum.</p>
	[[address]]
';

$text = str_ireplace('[[phone]]', '+1 (123) 000-00-00', $text);
$text = str_ireplace('[[address]]', 'New York, NY, 10120, USA', $text);
echo $text;
PHP

Result:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
+1 (123) 000-00-00
<p>Sed porta justo sed nibh elementum condimentum.</p>
New York, NY, 10120, USA

Shortcodes with content from an array

This functionality can be implemented using regular expressions using the preg_match_all() function.

$text = '
	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
	[[phone]]
	<p>Sed porta justo sed nibh elementum condimentum.</p>
	[[address]]
';

// Array of values
$info = array(
	'phone'   => '+1 (123) 000-00-00',
	'address' => 'New York, NY, 10120, USA',
);

// Find and replace
preg_match_all("|\[\[(.*)\]\]|U", $text, $matches);
if (!empty($matches[1])) {
	foreach ($matches[1] as $row) {
		$replase = (empty($info[$row])) ? '' : $info[$row];
		$text = str_ireplace('[[' . $row . ']]', $replase, $text);
	}
}

echo $text;
PHP

Result:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
+1 (123) 000-00-00
<p>Sed porta justo sed nibh elementum condimentum.</p>
New York, NY, 10120, USA
17.03.2018, updated 09.06.2021
35

Comments

to add a comment.

Other publications

Serialize function, possible problems
The serialize () function returns a string representation of any value (array, object, etc.). Using serialize on an...
46
0
How to set up Last-Modified
The Last-Modified header helps to optimize the loading of web pages and make the job easier for search engines.
134
0
How to find out if a request came via AJAX
To distinguish between direct and AJAX requests, simply check the value of the $ _SERVER global variable
78
0
Session lifetime in PHP
By default, in php, the session lifetime is only 24 minutes, and cookies are saved until the browser is closed, such parameters are set on many hosting services.
362
0
Writing and reading files in PHP
Examples of saving and reading text data and arrays to files.
179
0
Disable PHP caching
Sometimes on hostings PHP opcache caching is enabled by default. Edits were made in the code, but on the site everything is displayed as before...
67
0