Trying to post the content of a post WITHOUT the blockquote or images. My code takes out the images but still puts in the block quote and text.
<?php
$content = preg_replace('/<blockquote>(.*?)<\/blockquote>/', '', get_the_content());
$content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>
Simple error here - your second call to preg_replace() is using get_the_content(), which is the unmodified content - so you're basically doing away with what you did on your first line.
For the second line to use the output of the first line, your second parameter needs to be $content:
$content = preg_replace('/(<img [^>]*>)/', '', $content);
Related
I'm looking to separate the text and images in my Wordpress post. I want to be able to put them in different areas on my page.
I currently have only been able to get the content in tags but can't isolate and separate the content.
current html
<?php
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); // remove paragraphs around img tags
echo $content;
?>
Updated based on your comment.
You can get all the content with no images.
$content = get_the_content();
$text = wp_strip_all_tags( $content );
And to get the images you can get all the URLs inside of an array.
$re = '/<img .+? src="(.+?)"/i';
preg_match_all( $re, $content, $images );
// images url are located on $images[1]
I want to perform a three step process:
Check the first few characters (ffc) of a string variable.
If ffc = x (another string of characters) Then
Insert x after ffc but before any other content in ffc.
How can I accomplish this in PHP?
Actual Use Case:
I am using WordPress and grabbing the_content() and moving it into a variable $content.
I want some characters to appear before the text in $content, but WP auto adds <p> tags (if wpautop is on, and I'd like to avoid turning it off), which means the characters I add appear above rather than on the same line as $content.
The goal here is to check if $content starts with <p> and if it does to insert after <p> the characters "Summary: ".
Here is what I have thus far (it isn't working):
<?php
$content = get_the_content();
echo $content;
$hasP = substr($content, 0, 3);
echo $hasP;
If ($hasP == '<p>') {
echo "Yes!";
$newString = substr($string, 3);
echo $newString;
};
?>
Unfortunately, it seems that WP just re-adds the <p> when I echo $newString.
So, this took a long time for me to figure out, but here is the solution I came up with (err, make that stole from other people):
<?php
add_filter('the_content', 'before_after');
$content = the_content();
echo $content;
?>
And then the actual magic happens here:
function before_after($content) {
$content = preg_replace('/<p>/', '<span>', $content, 1);
$content = preg_replace('/<\/p>/', '</span>', $content, 1);
return $content;
}
In the above I actually went beyond what I had initially stated and replaced both the opening and closing <p>.
I want to pull out the list (ul) element from my wordpress post(s) so I can put it in a different location.
My current css pulls out the images and blockqute and puts just the text
html
<?php
$content = preg_replace('/<blockquote>(.*?)<\/blockquote>/', '', get_the_content());
$content = preg_replace('/(<img [^>]*>)/', '', $content);
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>
Just a friendly reminder is that it is generally not recommended to parse html with regex.
If you would like to do that anyway you could try like this:
$pattern = '~<ul>(.*?)</ul>~s';
So in your code it would look like this:
preg_match_all('/(~<ul>(.*?)</ul>~s)/', $content, $ulElements);
And then for removing it from the original string:
preg_replace('/(~<ul>(.*?)</ul>~s)/', '', $content);
I have the following code which puts the content of a Wordpress post onto the page (the images were removed)
<?php
$content = preg_replace('/<blockquote>(.*?)<\/blockquote>/', '', get_the_content());
$content = preg_replace('/(<img [^>]*>)/', '', get_the_content());
$content = wpautop($content); // Add paragraph-tags
$content = str_replace('<p></p>', '', $content); // remove empty paragraphs
echo $content;
?>
I want to remove the blockquote as well, which it does, HOWEVER placed where it currently is it does not remove the blockquote.
Placed after the 'img' line it DOES remove the block quote but the images are put back in
You should use
strip_tags — Strip HTML and PHP tags from a string
$string = get_the_content();
// remove all html tags
echo strip_tags($string);
// Allow specific tags such as <p> and <a>
echo strip_tags($string, '<p><a>');
To remove the styled quotation mark in the blockquote element, you'll need to modify the ::before pseudo-element, which contains: content: "
CSS to add:
blockquote::before {
content:none;
}
i am working on code where i upload html code and same code is added as content with top char being title and seo url.
but i had issue with making title as unable to get only plain text from html string to use it as title and seo url
below is my code to get title from html text:
$title = getplaintextintrofromhtml($str,100);
$title = str_replace(PHP_EOL, '', $title);
$title = str_replace(" "," ", $title);
$title = str_replace(str_split('\\/:*?"<>|,+=-'), '', $title);
$title = str_replace("'","", $title);
$title = str_replace("<br>","", $title);
$title = str_replace("\n","", $title);
$title = trim($title);
seo url
$newurltitle=str_replace(" ","-",$title);
and function
function getplaintextintrofromhtml($html, $numchars) {
// Remove the HTML tags
$html = strip_tags($html);
// Convert HTML entities to single characters
$html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
// Make the string the desired number of characters
// Note that substr is not good as it counts by bytes and not characters
$html = mb_substr($html, 0, $numchars, 'UTF-8');
// Add an elipsis
return $html;
}
even after my above code i get titles with new line , i could not figure out why this happens even thought i am getting plain text but issue like new line still there and i can not use them to make seo url also
You can use the following code to remove newlines, extra spaces, and line feeds:
$title = preg_replace('/\s+/', ' ', $title);