Skip to content Skip to sidebar Skip to footer

How To Remove Html Part Of A Text In Php

I have a question about parsing text and removing unwanted html parts. I know functions like - strip_tags() which will remove all the tags, but the problem is, that this function l

Solution 1:

Try this:

functionremoveTags($str) {
    $result = '';

    $xpath = new DOMXPath(DOMDocument::loadHTML(sprintf('<body>%s</body>', $str)));
    foreach ($xpath->query('//body/text()') as$textNode) {
        $result .= $textNode->nodeValue;
    }

    return$result;
}

echo removeTags(
    'Hello, how are you? <a href="">Link to my website</a> __Here continues html <span>tags</span>, links, images__'
);

Output:

Hello, how are you? __Here continues html , links, images__

Solution 2:

Why not make it a rule that the submittet input are not allowed to contain tags.

functioncontainsIllegalHtml($input, $allowable_tags = '') {
    if($input != strip_tags($input, $allowable_tags)) {
        returntrue;
    } else {
        returnfalse;
    }
}

Use this function to check wether the input contains tags or not.

Solution 3:

you may write a function that takes a string and it uses php string capabilities to get the position of the "<" and then the position of the ">" and strip them from the input string

Solution 4:

maybe its not correct, but...

$str = 'Hello, how are you? <a href="">Link to my website</a> __Here continues html tags, links, ';
$rez = preg_replace("/\<.*\>/i",'',$str);
var_dump($rez);

gave me an output

string'Hello, how are you?  __Here continues html tags, links, ' (length=56)

Solution 5:

i have searched and found this solution

$txt = "
<html><head><title>Something wicked this way comes</title></head><body>
This is the interesting stuff I want to extract
</body></html>";

$text = preg_replace("/<([^<>]*)>/", "", $txt);

echo htmlentities($text);

Post a Comment for "How To Remove Html Part Of A Text In Php"