PHP - remove <img> tag from string - php

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');

Related

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.

Replace DIVs without attributes with P

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

Removing Images from a WordPress Post

I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post which had an amazing little piece of code that solved my problem.
<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
echo $postOutput;
?>
But some time i have link above images like:
<img src="PATH_IMAGES">
So, How can remove it?
Change above code by following code which remove link if exist into your content.
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
$postContent = preg_replace("/<a[^>]+\>/i", "", $postOutput);
echo $postContent;

Using PHP is there a way to detect if there are two uninterrupted occurrences of an element?

This is a difficult question for me to word correctly, but I am trying to dynamically insert an ASIDE [specifically, just a "special thanks" note] between paragraphs. Initially I decided to drop this after the second paragraph by using substr_count(). I am floating this block, so if all it had to deal with was textual content there was no issue word-wrapping it. However, if it ran adjacent to an image or a PRE or anything else, it got wonky.
Anyway, what I want to do is detect when there is the first occurrence of TWO adjacent paragraphs and insert my aside between those. I.e.:
<p> Here is the first paragraph. </p>
<aside> INSERT THIS HERE </aside>
<p> Here is the second paragraph. </p>
Thoughts are appreciated.
Update: the substr_count() I am currently using.
Because I got voted down for not showing the original code, I'll post it below. I am using Wordpress but this isn't a WP specific question, as I'm taking the_content() as the string, counting the occurrences of P, and inserting the custom field there. This is ultimately not what I want to do, but I want to count two concurrent P's and insert this field between. It may be formatted strangely, as of course when I c/p from my editor stuff was all over the place.
$thanks = get_post_meta(get_the_ID(), 'thanks', true);
if (!$thanks) {
the_content();
}
else {
$show_after_p = 0;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $show_after_p) {
$contents = explode("</p>", $content);
$p_count = 0;
foreach($contents as $content)
{
echo $content;
if($p_count == $show_after_p)
{
?>
<aside class="thanks clearfix">
<p>
<?php echo '<span>Special thanks: </span>'.$thanks; ?>
</p>
</aside>
<?php }
echo "</p>";
$p_count++;
}
}
}
?>
Why not JS?
Well, javascript could definitely do this, but if I can do this in PHP then yahtzee.
use regular expression and preg_replace - search for: (closing of P){whitespaces (if any)}(opening P) and change whitespaces to your text.
something like that:
$content = preg_replace('#</p>\s*<p#', "</p><aside> INSERT THIS HERE </aside><p", $content);
Kudos to Jerzy Zawadzki for the inspiration. If you refer to the original code I segmented the content at the P tags using explode(). Now I am using preg_split() as follows:
$contents == preg_split('#</p>\s*<p>#', $content); // Thanks Jerzy
$i = 0;
foreach ( $contents as $content ) {
echo $content;
if ( $i == $show_after_p ) { // this is set to 0 right now
// insert aside here
}
echo "<p>";
$i++;
}
}
As far as I can tell, this busts up the content only at segments where there are concurrent paragraphs and no whitespace / images / other elements. At this first junction, I insert the aside. Then the content is appears as normal.

Get content in faster way from url using php

I am using php, I want to get the content from url in faster way.
Here is a code which I use.
Code:(1)
<?php
$content = file_get_contents('http://www.filehippo.com');
echo $content;
?>
Here is many other method to read files like fopen(), readfile() etc. But I think file_get_contents() is faster than these method.
In my above code when you execute it you see that it give every thing from this website even images and ads. I want to get only plan html text no css-style, images and ads. How can I get this.
See this to understand.
CODE:(2)
<?php
$content = file_get_contents('http://www.filehippo.com');
// do something to remove css-style, images and ads.
// return the plain html text in $mod_content.
echo $mod_content;
?>
If I do that like above then I am going in wrong way, because I already get the full content in variable $content and then modify it.
Can here is any function method or anything else which get the directly plain html text from url.
Below code is written only to understanding, this is not the original php code.
IDEAL CODE:(3);
<?php
$plain_content = get_plain_html('http://www.filehippo.com');
echo $plain_content; // no css-style, images and ads.
?>
If I can get this function it will be much faster than others. Can it is possible.
Thanks.
Try this.
$content = file_get_contents('http://www.filehippo.com');
$this->html = $content;
$this->process();
function process(){
// header
$this->_replace('/.*<head>/ism', "<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE html PUBLIC '-//WAPFORUM//DTD XHTML Mobile 1.0//EN' 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head>");
// title
$this->_replace('/<head>.*?(<title>.*<\/title>).*?<\/head>/ism', '<head>$1</head>');
// strip out divs with little content
$this->_stripContentlessDivs();
// divs/p
$this->_replace('/<div[^>]*>/ism', '') ;
$this->_replace('/<\/div>/ism','<br/><br/>');
$this->_replace('/<p[^>]*>/ism','');
$this->_replace('/<\/p>/ism', '<br/>') ;
// h tags
$this->_replace('/<h[1-5][^>]*>(.*?)<\/h[1-5]>/ism', '<br/><b>$1</b><br/><br/>') ;
// remove align/height/width/style/rel/id/class tags
$this->_replace('/\salign=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sheight=(\'?\"?).*?\\1/ism','');
$this->_replace('/\swidth=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sstyle=(\'?\"?).*?\\1/ism','');
$this->_replace('/\srel=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sid=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sclass=(\'?\"?).*?\\1/ism','');
// remove coments
$this->_replace('/<\!--.*?-->/ism','');
// remove script/style
$this->_replace('/<script[^>]*>.*?\/script>/ism','');
$this->_replace('/<style[^>]*>.*?\/style>/ism','');
// multiple \n
$this->_replace('/\n{2,}/ism','');
// remove multiple <br/>
$this->_replace('/(<br\s?\/?>){2}/ism','<br/>');
$this->_replace('/(<br\s?\/?>\s*){3,}/ism','<br/><br/>');
//tables
$this->_replace('/<table[^>]*>/ism', '');
$this->_replace('/<\/table>/ism', '<br/>');
$this->_replace('/<(tr|td|th)[^>]*>/ism', '');
$this->_replace('/<\/(tr|td|th)[^>]*>/ism', '<br/>');
// wrap and close
}
private function _replace($pattern, $replacement, $limit=-1){
$this->html = preg_replace($pattern, $replacement, $this->html, $limit);
}
for more - https://code.google.com/p/phpmobilizer/
you can use regular expression to delete css-script's tags and image's tags, just replace those codes with blank space
preg_replace($pattern, $replacement, $string);
for more detail of function go here: http://php.net/manual/en/function.preg-replace.php

Categories