在php中翻译javascript函数

时间:2014-09-24 10:08:01

标签: javascript php html

我在jS中有以下代码:

function stripHTMLWithLinks(c) {
    var BodyContents = /([\s\S]*\<body[^\>]*\>)([\s\S]*)(\<\/body\>[\s\S]*)/i ;
    var h = c.match(BodyContents);

  if (h != null && h[2]) {
        c = h[2]; 
  }

    c = c.replace(/<a\s.*?href\s*=\s*"(.*?)".*?>(.*?)<\/a>/gi, "$2 [$1]");
    c = c.replace(/<a\s.*?href\s*=\s*'(.*?)'.*?>(.*?)<\/a>/gi, "$2 [$1]");
    c = c.replace(/\/\/--\>/gi, "");
    c = c.replace(/(\n)/gi,"");
    c = c.replace(/(\r)/gi,"");
    c = c.replace(/<title[^\>]*\>[\s\S]*\<\/title\>/gi,"");
    c = c.replace(/<br\s*\/\s*>/gi,"\n");
    c = c.replace(/(<\/h.>|<\/p>|<\/div>)/gi, "$1\n\n");
    c = c.replace(/<[^>]+>/g,"");
    c = c.replace(/&lt;/g,"<");
    c = c.replace(/&gt;/g,">");
    c = c.replace(/&nbsp;/g," ");
    return c;
}

我需要一个能够提取身体内容的功能。我在这里看到的所有例子都有点固定在一个特定的例子上,我需要它在动态的基础上。这个功能可以为我做,我只需要在php中。

2 个答案:

答案 0 :(得分:0)

这应该很容易转码为PHP。 在PHP手册中查找preg_matchpreg_replace

答案 1 :(得分:-1)

尝试strip_tags

$html = "<body><a href='http://example.com'>Something</a> <b>big</b> is coming</body>";
echo strip_tags($html); // Something big is coming