在少数情况下使用php删除一些html标签

时间:2016-07-20 21:54:20

标签: php html tags

即时通讯使用php,我想知道如何删除<p class="xxx"></p>标签

来自:

<p class="xxx">
    <a href="xxx" target="xxx">
        <figure>
            <img src="xxx"/>
            <figcaption class="xxx">
                <h1 class="xxx">Text</h1>
                <cite class="xxx">Text</cite>
            </figcaption>
        </figure>
    </a>
</p>

到此:

<a href="xxx" target="xxx">
    <figure>
        <img src="xxx"/>
        <figcaption class="xxx">
            <h1 class="xxx">Text</h1>
            <cite class="xxx">Text</cite>
        </figcaption>
    </figure>
</a>

我想在<p></p>

时删除<p><a><figure><img/><figcaption><h1></h1><cite></cite></figcaption></figure></a></p>

我试试这个:

$html = preg_replace("'
(<p[^>]*>)([^<]*<a[^>]*>[^<]*<figure[^>]*>[^<]*<img[^>]*>[^<]*<figcaption[^>]*>[^<]*<h1[^>]*>[^<]*</h1[^>]*>[^<]*<cite[^>]*>[^<]*</cite[^>]*>[^<]*</figcaption>[^<]*[^<]*</figure>[^<]*[^<]*</a>[^<]*)(</p>)'sim", "$2", $valBody);
echo '<H1>Nuevo </H1><br>' . $html;

但我无法得到它,你可以帮助我。

1 个答案:

答案 0 :(得分:0)

如果你想用preg_replace()做,我就是这样做的:

import os
import io
import time
import zmq
from multiprocessing import Process

def connect_to_host(context, host, port, timeout):
    address = ("tcp://%s:%s" % (host, port))

    socket = context.socket(zmq.DEALER)

    socket.setsockopt(zmq.RCVTIMEO, timeout)
    socket.connect(address)

    print("Connecting to distribution server %s" % (address, ))

    while True:
        try:
            print("[Dist] Send")
            socket.send_multipart([str(0)])
            print("[Dist] Recv")
            port = socket.recv_multipart()[0]
            print("[Dist] %s" % (port, ))

            break
        except zmq.Again:
            socket.close()

            socket = context.socket(zmq.DEALER)

            socket.setsockopt(zmq.RCVTIMEO, timeout)
            socket.connect(address)

            print("Connecting to distribution server %s" % (address, ))

    socket.close()

    address = ("tcp://%s:%s" % (host, port))

    socket = context.socket(zmq.DEALER)

    socket.setsockopt(zmq.RCVTIMEO, timeout)
    socket.connect(address)

    print("Connecting to host %s" % (address, ))

    return socket

def client_task(client_type):
    timeout = 5000
    host = "vops-server.com"
    port = "7777"

    context = zmq.Context().instance()

    socket = connect_to_host(context, host, port, timeout)

    while True:
        try:
            try:
                print("[Host] Send")
                socket.send_multipart(["Hello, world!"])

                print("[Host] Recv")
                data = socket.recv_multipart()[0]

                print("[Host] %s" % (data, ))
            except zmq.Again:
                socket.close()

                socket = connect_to_host(context, host, port, timeout)

        except KeyboardInterrupt:
            break

    print("Connection terminated")

    socket.close()
    context.term()

if __name__ == "__main__":
    client_task(0)

此代码按照询问的方式输出输出:

<?php

$html =<<<HTML
<p class="xxx">
    <a href="xxx" target="xxx">
        <figure>
            <img src="xxx"/>
            <figcaption class="xxx">
                <h1 class="xxx">Text</h1>
                <cite class="xxx">Text</cite>
            </figcaption>
        </figure>
    </a>
</p>
HTML;

$pattern = '#(<p.+">)#';
$replace = '';
$html = preg_replace($pattern, $replace, $html);
$pattern = "#(</p>)#";
$replace = '';
$html = preg_replace($pattern, $replace, $html);
$pattern = "#(\s\s)+#";
$replace = '';
$html = preg_replace($pattern, $replace, $html);

echo '<H1>Nuevo </H1><br>' . $html;

?>

我是一名新手程序员,这是我在Stack Overflow上的第一个答案。最好的问候。