Regex add character between two strings
Posted: Thu Aug 22, 2013 4:39 am
Hi,
I have the following string: ====== Text ======. As the number of the = characters may be from 2 to 6 on both sides of the text. I want to insert a dash like this: ====== -Text ======.
For this purpose I use the following code:
However, when I have some other string like this: ===============================, then a regex add dashes and in it.
I want to add dashes only when there is text between =(equal signs)
Regards!
I have the following string: ====== Text ======. As the number of the = characters may be from 2 to 6 on both sides of the text. I want to insert a dash like this: ====== -Text ======.
For this purpose I use the following code:
Code: Select all
$pattern = "/(={2,6})(.+?)(={2,6})/";
$replace = '$1 -$2$3';
$content = preg_replace($pattern, $replace, $content);
I want to add dashes only when there is text between =(equal signs)
Regards!