I have this code
$regex = '/{tip(?:\s+class\s*=\s*"([a-zA-Z\s]+)")?}([^{]*){\/tip}/';
$matches = null;
preg_match_all($regex, $article, $matches);
if(is_array($matches)) {
foreach ($matches as $match) {
$article = preg_replace($regex, '<span class="tooltip $1"><i>?</i><em>$2</em></span>', $article);
}
}
So I'm looking for {tip} tip text {/tip}
, or {tip class="someClass"} tip text{/}
and replacing it. Its works fine, but depending on class there will be different output. I need something like:
if (strpos($match, 'class') !== false) {
output
} else {
$class = $match[0];
if ($class == 'sticked') {
other output
}
}
But i dont know how to extract class to variable, also match is an array of results, so class will be on different position every time.
Try this:
$regex = '/{tip\s*([a-z]*)=?"?\'?([^\'"]*)"?\'?\s*}([^{]*){\/tip}/';
This way you can extract the class property with any number of spaces before or after it with \s* and with single or double quotes with "?'?
Edited to work with/without class definition. This pattern will return the property being used, so you'll still get a match if "class" is misspelled.