Home > php教程 > PHP开发 > body text

Regular expression, a simple example of replacing all HTML tags

高洛峰
Release: 2016-12-05 09:32:57
Original
1354 people have browsed it

I wrote a regular expression myself, <(.|n)+?>

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;
    }
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template