To remove special characters like <p>, <li> - php

I want to display content on client side. The problem is that I am getting output like this -
<p> Aliette is a systemic fungicide effective against Oomcytes fungi like downy mildew
diseases of grapes and damping off and Azhukal diseases of cardamom.</p> <span> Despite
its extensive use since 1978, there is no report of resistance development in fungus.
True systemic action makes application of Aliette as the best prophylactic solution for
downy mildew control in grape.</span>
Now I want to remove those special characters i.e <p>,</p>, <span>, </span>
Value stored in database is description = "<p> test <p>";
$sel_pro = "select * from bayer_product where product_group like '%".$_REQUEST['searchfield']."%'";
$res_pro = mysql_query($sel_pro);
$num_pro = mysql_num_rows($res_pro);
while($row_pro = mysql_fetch_assoc($res_pro))
{
echo $desc = strip_tags($row_pro['description']);
}

If they are tags then you can use strip_tags().
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
the outputs will be
Test paragraph. Other text //strip all tags
<p>Test paragraph.</p> Other text //strip all tags except <p> & <a>

For clean all html tags in text use strip_tags() function. Usage you can see in the docs. If you need to clean all tags, except few that you need - simply put "allowable_tags" param.
http://www.nusphere.com/kb/phpmanual/function.strip-tags.htm

Use PHP function strip_tags for remove tag
<?php
$text = '<p> Aliette is a systemic fungicide effective against Oomcytes fungi like downy mildew
diseases of grapes and damping off and Azhukal diseases of cardamom.</p> <span> Despite
its extensive use since 1978, there is no report of resistance development in fungus.
True systemic action makes application of Aliette as the best prophylactic solution for
downy mildew control in grape.</span>';
echo strip_tags($text);
echo "\n";
// Autorise <p> et <a>
echo strip_tags($text, '<p><span>');
?>

Related

Closing and opening <p> tags

I have a function that wraps <img> tags in a <div>. Often these can appear in <p> tags (due to a wysiwyg editor).
$doc->loadHtml($str);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
$div = $doc->createElement('div');
$tag->parentNode->insertBefore($div, $tag);
$div->appendChild($tag);
}
I want to change my function so that if there is an open <p> tag, it appends a closing </p> tag before the opening <div>, and then adds an opening <p> after the closing </div>.
Currently when input is
'<p>blah blah <img src="incorrect.gif"> blah blah</p>
My output is
'<p>blah blah <div><img src="incorrect.gif"></div> blah blah</p>
I want output to be
'<p>blah blah </p><div><img src="correct.gif"></div><p> blah blah</p>
There are two approaches coming to my mind:
instead of wrapping images with <div>...</div>, wrap them with </p><div>...</div><p>
detect the div tags and wrap them with </p>...<p>
If you are certain that the wrapping you use around images is always a <div>...</div> pair, you might consider using regex to do a simple replace:
PHP
$html = preg_replace_all('/<div[^>]*>(.*?)</div>/i', '</p>$1<p>', $html)
This will also work if you include attributes in your wrapping div elements. However, if you intend on starting to use regex for more complex operations on HTML, here is a previous post that details why it is a bad idea:
Using regular expressions to parse HTML: why not?
You can use preg_replace function to achieve this.
$html ='<p>blah blah <div><img src="incorrect.gif"></div> blah blah</p>';
echo preg_replace("/<p>(.*?)<div>(.*?)<\/div>(.*?)<\/p>/", "<p>$1</p><div>$2</div><p>$3</p>", $html);
I have tested couple of case although check using couple others

Using preg_replace to match html tag style-attributes

I have the following regex:
preg_replace('#<(/?(?:pre|p|b|em|u|ul|li|ol|blockquote|h1|h2|h3|h4|h5|strong|br))>#', '<\1>', $this->body);
It works fine for replacing tags without style-attributes like:
<p>, <b>, <li>
However I am trying to make it match tags with style attributes also, like:
<p style="margin-left: 20px"> and <p style="text-align: right;">.
How can I do that?
Thank you for all your help!
Try this:
<(/?(?:pre|p|b|em|u|ul|li|ol|blockquote|h1|h2|h3|h4|h5|strong|br))(?: +[a-z]+="[^"]*")*>
The last part:
(?: +[a-z]+="[^"]*")*
matches attributes="xxxxx"
Form comment : I need only the style attribute - and only for <p>-tags!
Get the matched group from index 1.
<p.*(style="[^"]*")[^>]*>
Online demo
Form comment :All other attributes than the style attribute should be omitted!
Try below sample code to substitute it p tag only with style attribute.
sample code:
$re = "/<p.*(style=\\"[^\\"]*\\")[^>]*>/m";
$str = "<p style=\"margin-left: 20px\" align=\"left\">\n<p align=\"left\">\n<p align=\"left\" style=\"margin-left: 20px\">\n";
$subst = '<p $1>';
$result = preg_replace($re, $subst, $str);
Find detail description on Grabbing HTML Tags

Regular expression from wysiwyg

I have HTML-code, that come from user who use wysiwyg redactor.
I need to сlean code from tags like <b ..><i ..><strong><p><a ..>, and clean up from all main js code, like onclick and other.
Thanks.
Use strip_tags to remove html from text. Example below.
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";

How to get everything between <span> & </span> including tags and text

I tried using preg_match_all to get all the contents between a given html tag but it produces an empty result and I'm not good at php.
Is there a way to get get contents between tags? Like this -
<span class="st"> EVERYTHING IN HERE INCLUDING TAGS<B></B><EM></EM><DIV></DIV>&+++ TEXT </span>
preg_match is not very good at HTML parsing, especially in your case which is a bit more complex.
Instead you use a HTML parser and obtain the elements you're looking for. The following is a simple example selecting the first span element. This can be more differentiated by looking for the class attribute as well for example, just to give you some pointers for the start:
$html = '<span class="st"> EVERYTHING IN HERE INCLUDING TAGS<B></B><EM></EM><DIV></DIV>&+++ TEXT </span>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$span = $doc->getElementsByTagName('span')->item(0);
echo $doc->saveHTML($span);
Output:
<span class="st"> EVERYTHING IN HERE INCLUDING TAGS<b></b><em></em><div></div>&+++ TEXT </span>
If you look closely, you can see that even HTML errors have been fixed on the fly with the &+++ which was not valid HTML.
If you only need the inner HTML, you need to iterate over the children of the span element:
foreach($span->childNodes as $child)
{
echo $doc->saveHTML($child);
}
Which give you:
EVERYTHING IN HERE INCLUDING TAGS<b></b><em></em><div></div>&+++ TEXT
I hope this is helpful.
Try this with preg_match
$str = "<span class=\"st\"> EVERYTHING IN HERE INCLUDING TAGS<B></B><EM></EM><DIV></DIV>&+++ TEXT </span>";
preg_match("/<span class=\"st\">([.*?]+)<\/span>/i", $str, $matches);
print_r($matches);

Replace pagebreak tag with regular expression

Here's what I am tryin to accomplish. The CMS editor of our Magento webshop, has a button to insert a <!-- pagebreak --> tag. I would like to use this, to create a read more functionality. I thought I would search/replace for this tag to do this.
I want to search inside <p> tags, and I want people to be able to use this tag as often as they want.
Suppose this is my original HTML:
<p>This is my example text, but<!-- pagebreak --> this should be readable after 'click more'<!-- pagebreak --> with even more click more possible</p>
I would like to convert it to something like this.. I think the first one is the easiest to accomplish, maybe by doing an preg_replace in a while loop? The second one is probably cleaner/better html (less nesting)
<p>This is my example text, but <a href="#" onClick='#'>read more</a><div class='hiddenreadmore' id='hiddenreadmore-1'> this should be readable after 'click more'<a href="#" onClick='#'>read more</a><div class='hiddenreadmore' id='hiddenreadmore-2'> with even more click more possible</div></div></p>
or
<p>This is my example text, but <a href="#" onClick='#'>read more</a><div class='hiddenreadmore' id='hiddenreadmore-1'> this should be readable after 'click more'<a href="#" onClick='#'>read more</a></div><div class='hiddenreadmore' id='hiddenreadmore-2'> with even more click more possible</div></p>
So I came up with this, but I think there should be a way to do it with one replace.
$pattern = '#\<p\>(.+?)\<\!-- pagebreak --\>(.+?)\<\/p\>#s';
$count = true;
while ($count) {
$text = preg_replace($pattern, '<p>$1 read more<div class="hidden">$2</div></p>', $text, -1, $count);
}
Well if it you dont need to check if it's in a <p> tag you can use something like this:
str_replace ( "<!-- pagebreak -->" , '<p>$1 read more<div class="hidden">$2</div></p>' , $text, $count );
It's a lot lighter to the system.
I guess this would do the job:
$pattern = '#\<p>(.*?)\<!-- pagebreak -->(.*?)\</p>#s';
$text = "<p>some test <!-- pagebreak --> hidden content</p> second test <p>lolo <!-- pagebreak --> more hidden content</p>";
echo preg_replace($pattern, '<p>$1 read more<div class="hidden">$2</div></p>', $text, -1, $count);

Categories