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 get last matched occurrence in PHP regex?
- How to use PHP regex to match a multiline?
- How to convert a PHP regex to JavaScript regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a boolean value?
- How to use PHP regex to match special characters?
- How to use PHP regex to match a zip code?
- How to use PHP regex to match an exact string?
- How to use backslash in PHP regex?
- How to use PHP regex with zero or more occurrences?
See more codes...