Home  >  Article  >  php教程  >  Regular expression, a simple example of replacing all HTML tags

Regular expression, a simple example of replacing all HTML tags

高洛峰
高洛峰Original
2016-12-05 09:32:571328browse

I wrote a regular expression myself, cf58df36d0d993dc3e7855f859d4d20e

This is to replace all HTML tags, non-greedy, multi-line.

If I want to replace all non-HTML tags,

My code can only be like this, first find the HTML tag, and then replace the tag.

Can I find non-HTML tags directly? .

There is another problem, intercepting the length of the string.

The method I used below does not determine whether it is Chinese or non-Chinese. The intercepted length is always longer or shorter.

I don’t know if there is a better way to make the intercepted length the same length, instead of the length of str.Length.

public static string formatString(string str, int size)
    {
      string temp = str;
 
      Regex regex = new Regex("<.+?>");
 
      temp = regex.Replace(str, "");
 
      temp = temp.Replace("\r\n", "");
 
      temp = temp.Replace(" ", "");
 
      if (temp.Length >= size)
      {
        temp = temp.Substring(0, size - 3) + " ";
      }
 
      return temp;
    }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn