How would I convert strings like this:
<span class="it">CONTENT</span>
Into this:
{it}CONTENT{/it}
While keeping CONTENT intact?
The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a replacement string.
str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f. {2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.
PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.
preg_replace('/<span class="it">(.*?)<\/span>/', '{it}$1{/it}', $text)
This is not the most versatile solution, but this works for your code. There is the possibility to have the content of the class attribute as a variable as well, but that won't be too hard to figure out now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With