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
24

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...
30
0
Examples of using cURL in PHP
cURL PHP is a library for receiving and transmitting data via protocols such as HTTP, FTP, HTTPS. The library is used...
316
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.
97
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
60
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.
145
0
Working with PHP arrays - creating, filling, deleting
Basic examples of working with PHP arrays. Creation, filling, extraction and removal of values.
105
0