Replace DIVs without attributes with P - php

I have a string which contains html.
Example:
$content="<div>content<div style=''>some<div>another</div></div> <div>test</div> </div>";
I want to replace all divs without attributes, to paragraphs.
I tried
$content = preg_replace( '/<div>(.*?)<\/div>/', '<p>$1</p>', $content);
but it returns:
<p>content<div style=''>some<div>another</p></div> <p>test</p> </div>
which is not what I want. I want to replace all div without attributes to p.
What should I do?
Thank you!

$content = str_replace("<div>","<p>",$content);
$content = str_replace("</div>","</p>",$content);
echo $content

Related

Remove the opening and closing div inside WordPress content

I want to remove a div added by a plugin to the content of WordPress posts. So the post has this structure:
<div class="post">
<div class="some-class">
<p>content</p>
</div>
</div>
I want to remove <div class="some-class"> and its closing </div> but leave the content. So it would be:
<div class="post">
<p>content</p>
</div>
using this filter:
add_filter( 'the_content', 'remove_class' , 100 );
function remove_class( $content ) {
$content = preg_replace('#<div[^>]*class="some-class"[^>]*>.*?</div>#is', '', $content);
return $content;
}
the content is also deleted, I just want the div and the closing div to be deleted. Any idea how?
this question is not duplicate of the other question because I want to remove a specific div not just all divs
You could just try to remove class attribute, so that only <div> is left, using code like this:
add_filter( 'the_content', 'remove_class' , 100 );
function remove_class( $content ) {
$content = preg_replace('/class=".*?"/', '', $content);
return $content;
}
#user7592255 you can try with jQuery like this:
$('p').unwrap();
If you can set an id or class on the p element you can target it more accurately
The content is removed because you replace the entire matched string with an empty string. Use a subpattern to capture the content of the <div> element and use it as replacement:
$content = preg_replace(
'#<div[^>]*class="some-class"[^>]*>(.*?)</div>#is',
'$1',
$content
);
However, be aware that it won't work properly if the content of <div class="some-class"> contains a <div> element.
There is no way to parse HTML using regex. The correct solution is to use an HTML parser (DOMDocument f.e.) to parse the HTML fragment and create its DOM, then operate the changes on the DOM and render it back to HTML.

Display Joomla Intro Article without <p> tag

I'm customizing the layout of category blog view for my template and I need to display the article intro text without the tag. In my custom file "blogalternativelayout_item.php", I use:
<?php echo substr(($this->item->introtext),0,75); ?>
Anyway this renders the introtext as
<p>Lorem ipsum etc...</p>
How could I do to remove the paragraph tags?
Thanks in advance.
You can use php strip_tags() function. eg;
echo strip_tags($this->item->introtext);
the code above will strip all the html tags in introtext.
If you want to strip tags except tags, then you can put it like this:
echo strip_tags($this->item->introtext, "<a>");
You have to use regex to achieve this task
<?php
$text = substr(($this->item->introtext),0,75);
//get the contents inside <p> tag using this regex
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $text);
echo $result;
?>
Thanks to both suggestions. I've created this code and working:
<?php
$desctrunc = substr(($this->item->introtext),0,75);
$desc = strip_tags($desctrunc);
echo $desc . '...';
?>
Thanks.

remove a child element in a element content with simple_html_dom

This is a sample html code:
<div class="leadContent">
<span> sentence 1 </span>
sentence 2
</div>
I just want to get sentence 2 (Not span tag and its content)
Is there any way to do this with simple_html_dom?
$html->find('div.leadContent', 0)->innertext;
You can remove span tag using outertext:
$html->find('div.leadContent span', 0)->outertext = '';
then you get the content
$html->find('div.leadContent', 0)->innertext;
edit:
You can also try this way:
$div = $html->find('div.leadContent', 0);
$div->find('span',0)->outertext = '';
$div will have your content.
You can try this way also,
echo $html->find('text',2)->plaintext;

create anchors in a page with the content of <h2></h2> in PHP

Well I have a html text string in a variable:
$html = "<h1>title</h1><h2>subtitle 1</h2> <h2>subtitle 2</h2>";
so I want to create anchors in each subtitle that has with the same name and then print the html code to browser and also get the subtitles as an array.
I think is using regex.. please help.
I think this will do the trick for you:
$pattern = "|<h2>(.*)</h2>|U";
preg_match_all($pattern,$html,$matches);
foreach($matches[1] as $match)
$html = str_replace($match, "<a name='".$match."' />".$match, $html);
$array_of_elements = $matches[1];
Just make sure that $html has the existing html before this code starts. Then it will have an <a name='foo' /> added after this completes, and $array_of_elements will have the array of matching text values.

PHP - remove <img> tag from string

Hey, I need to delete all images from a string and I just can't find the right way to do it.
Here is what I tryed, but it doesn't work:
preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
Any ideas?
Try dropping the \ in front of the >.
Edit: I just tested your regex and it works fine. This is what I used:
<?
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
The result is:
this is something with an (image) in it.
You need to assign the result back to $content as preg_replace does not modify the original string.
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
I would suggest using the strip_tags method.
Sean it works fine i've just used this code
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
echo $content;
//the result it's only the plain text. It works!!!
I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!
$news = $row_latest_news ['content'];
$news = preg_replace("/<img[^>]+\>/i", "", $news);
if (strlen($news) > 300){
echo substr($news, 0, strpos($news,' ',300)).'...';
}
else {
echo $news;
}
$this->load->helper('security');
$h=mysql_real_escape_string(strip_image_tags($comment));
If user inputs
<img src="#">
In the database table just insert character this #
Works for me
simply use the form_validation class of codeigniter:
strip_image_tags($str).
$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');

Categories