Add html tag in string php
This was a cool question because it promoted thought about the DoM. I raised a question How do HTML Parsers process untagged text which was commented generously by @sideshowbarker, which made me think, and improved my knowledge of the DoM, especially about text nodes. Below is a DoM based way of finding candidate text nodes and padding them with 'p' tags. There are lots of text nodes that we should leave alone, like the spaces, carriage returns and line feeds we use for formatting (which an "uglifier" may strip out).
nodeTest.html contains this.
and the output is this.... I did not bother echoing the outer tags. Notice that comments and formatting are properly treated.
Obviously you need to traverse the DoM and repeat the search/replace at each element node if you wish to make the thing more general. We are only stopping at the Body node in this example and processing each direct child node. I'm not 100% sure the code is the most efficient possible and I may think some more on that and update if I find a better way. ❮ PHP String Reference ExampleStrip the string from HTML tags: echo strip_tags("Hello world!"); Try it Yourself » Definition and UsageThe strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe. SyntaxParameter Values
Technical Details
More ExamplesExampleStrip the string from HTML tags, but allow tags to be used: echo strip_tags("Hello world!",""); Try it Yourself » ❮ PHP String Reference
Add html tag to string in PHP
function htmlParser($html) { foreach ($html->childNodes() as $node) { if ($node->childNodes()) { htmlParser($node); } // Ideally: add p tag to node innertext if it does not wrapped with any tag } return $html; } loadHTML($html); // build the DoM $bodyNodes = $dom->getElementsByTagName('body'); // returns DOMNodeList object foreach($bodyNodes[0]->childNodes as $child) // assuming 1 node { $text=""; // this tests for an untagged text node that has more than non-formatting characters if ( ($child->nodeType == 3) && ( strlen( $text = trim($child->nodeValue)) > 0 ) ) { // its a candidate for adding tags $newText = "
function addPTag($html) { $contents = preg_split("/(<\/.*?>)/", $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($contents as &$content) { if (substr($content, 0, 1) != '<') { $chars = preg_split("/(<)/", $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $chars[0] = ' $stringHtml = 'Your received html'; $html = str_get_html(stringHtml); //Find necessary element and edit it $exampleText = $html->find('Your selector here', 0)->last_child()->innertext Insert html tag in string in phpMango Orange Banana ', 'Mango, Orange, Banana'); ?>
How to display HTML tags as plain text using PHPstring htmlspecialchars( $string, $flags, $encoding, $double_encode ) string htmlentities( $string, $flags, $encoding, $double_encode ) Adding html tags to a string programmaticallyI need to put together a couple of functions that do the following:- $string = 'This is a string.';
$first_letter = substr($string, 0, 1);
$remaining_string = substr($string, 1);
if(substr($string, 0, 5) != '" .$first_letter.'< span>'.$remaining_string;
}
if(substr($string, -7) != '') {
$string .= '';
}
$string = 'This is a string.'; if(substr($string, 0, 5) != '" .$string; } if(substr($string, -7) !="" ) { $string .="" ; } < pre>span.some_class:first-letter { /* some styling */ } |