i want to delete the following type of expressions from my script.
<a any text here>nothing or space here</a>
i can do it by three functions, but i think there is a way shorter. could you help me? thanks in advance
i want to delete the following type of expressions from my script.
<a any text here>nothing or space here</a>
i can do it by three functions, but i think there is a way shorter. could you help me? thanks in advance
Will preg_replace('/<a(.*?)>\s*<\/a>/', '', $str)
work?
EDIT: Alan Moore is right.
Taken from http://www.regular-expressions.info/php.html:
mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
returns a string with all matches of the regex pattern in the subject string replaced with the replacement string.
At most limit replacements are made. One key difference is that all parameters, except limit, can be arrays instead of strings.
In that case, preg_replace
does its job multiple times, iterating over the elements in the arrays simultaneously. You can also use strings for some parameters, and arrays for others. Then the function will iterate over the arrays, and use the same strings for each iteration. Using an array of the pattern and replacement, allows you to perform a sequence of search and replace operations on a single subject string. Using an array for the subject string, allows you to perform the same search and replace operation on many subject strings.
$text = '<a any text here>nothing or space here</a>';
$rep = '';
$pat = '|<a[^>]*>\S*</a>|';
echo preg_replace($pat,$rep,$text);
EDIT: the wrong one
$text = '<a any text here>nothing or space here</a>';
$rep = '<a>\1</a>';
$pat = '|<a[^>]*>([^<]*)</a>|';
echo preg_replace($pat,$rep,$text);