Using the following code to remove images (and paste just the text)
<?php
$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Trying to bring in the images (in a new area of the same page) I a trying to use
preg_match('#(<img.*?>)#', $content, $results);
To no avail. And I am also wondering if there is a better way than to run a function twice, or If I can separate the text and image and call them into two different divs/locations
Get Only text Based content
function custom_strip_image($text) {
$text = preg_replace("/<img[^>]+\>/i", "", $text);
return $text;
}
echo custom_strip_image(get_the_content());
Make second function for get images list
Related
I am trying to put just the images that are in my Wordpress post on screen. I have the following code which removes them and posts just the text
<?php
$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
I'm basically trying to do the same thing but pull out and post just the images. These code(s) would appear on the same page so I'm not sure if it's possible to write one function to 'separate' the images and text and echo them n different divs.
I have a Wordpress site and I want to remove the block quotes from the post and put just the regular text. (also want to remove any images that are in the text, I just want the regular text)
This code does the OPPOSITE of what I want - takes out the block quotes and posts that. I want it to post the other text and NOT the block quote.
<?php
// get the content
$block = get_the_content();
// check and retrieve blockquote
if(preg_match('~<blockquote>([\s\S]+?)</blockquote>~', $block, $matches))
// output blockquote
echo $matches[1];
?>
What you need is a content filter. Add the following to your functions.php file
add_filter( 'the_content', 'rm_quotes_and_images' );
function rm_quotes_and_images($content)
{
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = preg_replace("/<img[^>]+>/i", "", $content);
return $content;
}
Try this
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
return $content;
}
Just add this to your code:
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
echo $content;
The way wali hassan sayed is to add following code to your function.php:
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
return $content;
}
This overrides the default "the_content()" function so in you page template you only need to call:
the_content();
I'm using the Simple HTML DOM Parser to retrieve a specific div from a website. I remove the part of the div that I don't want by using explode(). I then want to explode the kept part into a new array, but for some reason it doesn't get indexed as intended.
Why doesn't my last row with "echo $content[0];" print "Overall" while "echo $content[5];" does, when Overall is the first string? How do I fix this?
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur');
$content = $html->find('div[id=contentHiscores]', 0)->plaintext;
echo $content;
echo "<br><br><br><br>";
$content = explode("SkillRankLevelXP", $content);
$content = $content[1];
echo $content;
echo "<br><br><br><br>";
$content = explode(" ", $content);
echo $content[0];
?>
Between SkillRankLevelXP and Overall, there are 6 spaces, though the browser only shows it as 1. Use the "View Source" menu and you'll see what I mean.
You can use some RegEx to replace 2 or more spaces with just 1 space, and I think that will get you closer to what you want.
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://services.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=Pur');
$content = $html->find('div[id=contentHiscores]', 0)->plaintext;
$content=preg_replace('/ {2,}/', ' ', trim($content));
$content = explode('SkillRankLevelXP ', $content);
$content = $content[1];
$content = explode(' ', $content);
print_r($content);
?>
I am trying to customize a Word press template and having some trouble. The page is http://rexonmedia.com/?page_id=113 and under each section there are description, all the text in description is plain text(Actionscript, Visit site ...). I want the text to be formatted or have links like the source (http://rexonmedia.com/?portfolio=audiomaya) with hyperlink.
I came to find out that the culprit here is "strip_tags" tags. It's used on two different places. I tried couple of scenarios but didn't work. Please help
public function customFormat($content,$strip_tags = false,$shortcode=true){
$content = stripslashes($content);
if($shortcode)
$content = do_shortcode( shortcode_unautop( $content ) );
$content = preg_replace('#^<\/p>|^<br\s?\/?>|<p>$|<p>\s*( )?\s*<\/p>#', '', $content);
if($strip_tags)
$content = strip_tags($content,"<hades>,<tabend>");
return $content;
}
<?php
global $more; // Declare global $more (before the loop).
$more = 1;
$content = get_the_content('');
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$this->shortenContent( $content_limit , strip_tags( $content ) );
?>
Supply strip_tags with the allowable tags. The line in the first block of code would change to:
$content = strip_tags($content,"<strong><a>");
And the line in the second block of code would change to:
$this->shortenContent( $content_limit , strip_tags( $content, "<strong><a>" ) );
By the way, the function defines $strip_tags as false in the function definition if it is not supplied. Check that!
public function customFormat($content,$strip_tags = false,$shortcode=true){
Want to replace some words on the fly on my website.
$content = preg_replace('/\bWord\b/i', 'Replacement', $content);
That works so far. But now i want only change the the words which are inside
div id="content"
How do i do that?
$dom = new DOMDocument();
$dom->loadHTML($html);
$x = new DOMXPath($dom);
$pattern = '/foo/';
foreach($x->query("//div[#id='content']//text()") as $text){
preg_match_all($pattern,$text->wholeText,$occurances,PREG_OFFSET_CAPTURE);
$occurances = array_reverse($occurances[0]);
foreach($occurances as $occ){
$text->replaceData($occ[1],strlen($occ[0]),'oof');
}
//alternative if you want to do it in one go:
//$text->parentNode->replaceChild(new DOMText(preg_replace($pattern,'oof',$text->wholeText)),$text);
}
echo $dom->saveHTML();
//replaces all occurances of 'foo' with 'oof'
//if you don't really need a regex to match a word, you can limit the text-nodes
//searched by altering the xpath to "//div[#id='content']//text()[contains(.,'searchword')]"
use the_content filter, you can place it in your themes function.php file
add_filter('the_content', 'your_custom_filter');
function your_custom_filter($content) {
$pattern = '/\bWord\b/i'
$content = preg_replace($pattern,'Replacement', $content);
return $content;
}
UPDATE: This applies only if you are using WordPress of course.
If the content is dynamically driven then just echo the return value of $content into the div with id of content. If the content is static then you'll have to either use this PHP snippet on the text then echo out the return into the div, or use JavaScript (dirty method!).
$content = "Your string of text goes here";
$content = preg_replace('/\bWord\b/i', 'Replacement', $content);
<div id="content">
<?php echo $content; ?>
</div>