php-regexHow to remove a tag from a string using PHP regex?
Using PHP regex, you can remove a tag from a string by using the preg_replace()
function. This function takes two parameters, the first being the pattern to search for and the second being the replacement string.
For example, to remove a tag from a string, you can use the following code:
$string = '<p>This is a string with a tag.</p>';
$string = preg_replace('/<[^>]*>/', '', $string);
The code above will search for any tag in the string and replace it with an empty string. The output of the code above will be:
This is a string with a tag.
The code consists of two parts:
- The pattern to search for:
/<[^>]*>/
- This pattern searches for any tag in the string.
- The replacement string:
''
- This is an empty string which will replace any tag found in the string.
Helpful links
More of Php Regex
- How to match a space using PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match special characters?
- How to use PHP regex to match a year?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match a hashtag?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match URL path?
- How to use named capture groups in PHP regex?
See more codes...