This works perfectly for what I'm using it for (auto meta description and og:description content for WordPress), but I wonder if there is a way to write it shorter/cleaner:
$content = $post -> post_content;
$content = wp_trim_words($content, 40, '...'); // 40 words
$content = trim(str_replace(' ','',$content));
$content = do_shortcode($content);
$content = html_entity_decode($content);
$content = strip_tags($content);
$content = preg_replace('/\s\s+/', '', $content);
Updated
Okay, I think this will do the trick. I wanted to be able to use it again and again and then I thought, hey maybe it's like js. I'm a visual designer in my brain so it takes me sometime to get the hang of this stuff.
function do_meta_description_cab() {
global $post;
$content = $post -> post_content;
$content = wp_trim_words($content, 40, '...'); // 40 words
$content = trim(str_replace(' ','',$content));
$content = do_shortcode($content);
$content = html_entity_decode($content);
$content = strip_tags($content);
$content = preg_replace('/\s\s+/', '', $content);
return $content;
}
//Usage: $content = do_meta_description_cab();
To strip all html opening and closing tags, you can use :
$content = preg_replace('/<[^>]+>|<\/[^>]+>/', '', $content);
Related
I want to minimize html code dynamically when rendering
This is my view class and a page is rendered
public static function render($filePath, $data = array()){
extract($data);
ob_start();
require_once("mvc/view/" . $filePath);
$content = ob_get_clean();
minifyContent($content);
require_once("theme/default.php");
}
I try to use this function to minimize
function minifyContent($content){
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array(
'>',
'<',
'\\1'
);
$content = preg_replace($search, $replace, $content);
return $content;
}
When I use this, only my content shrinks and repeats twice
How can I tell him to minify all the content, even the default template?
I am trying to remove extra whitespace of a string I get from internet using file_get_contents(). I tried str_replace() andpreg_replace(), and also search but none of them worked.
Here is my code:
<?php $html_content = file_get_contents("http://mindcity.sina.com.hk/MC-lunar/daily/2014/12/20141209_b5.html");
$html_content = mb_convert_encoding($html_content, 'UTF-8', 'BIG-5');
$html_content = strip_tags($html_content);
$start_pos = strrpos($html_content, "宜 :");
$end_pos = strrpos($html_content, "凶神宜忌 :") - strlen($html_content);
$good_to_do = substr($html_content, $start_pos, $end_pos);
echo $good_to_do .'<br>';
//remove whitespace of $good_to_do
$good_to_do = str_replace(' : ','*',$good_to_do);
$good_to_do = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $good_to_do);
$good_to_do = str_replace(array("\r\n", "\r", "\n", "\t", "\0", "\s", "\x0B", "\x20", "\xA0"), '*', $good_to_do);
var_dump( $good_to_do ); ?>
Do
$good_to_do = preg_replace('/\s+/', '*', $good_to_do);
I put '*' because that is what you want to replace it with? You can put anything you like there.
Just found that the white space was when view source.
So the code just simply becomes str_replace(' ', '', $html_content);
i am getting data from website and i am saving to xml file. i want to replace all turkish characters because if not, i am getting weird characters when i access the data from xml.
i used this code
$html = str_replace("ç", "Bccedil;", $html);
$html = str_replace("Ç", "BCcedil;", $html);
$html = str_replace("ö", "Bouml;" , $html);
$html = str_replace("Ö", "BOuml;" , $html);
$html = str_replace("ý", "B#305;" , $html);
$html = str_replace("Ý", "B#304;" , $html);
$html = str_replace("ü", "Buuml;" , $html);
$html = str_replace("Ü", "BUuml;" , $html);
$html = str_replace("ð", "B#287;" , $html);
$html = str_replace("Ð", "B#286;" , $html);
$html = str_replace("þ", "B#351;" , $html);
$html = str_replace("Þ", "B#350;" , $html);
$myFile = "test.xml";
$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, $html);
fclose($fh);
it is weird that nothing changed after this code. if i want to replace for example 'a' to 'c' it works. but for turkish characters it doesnt work.
Data i get :
İstanbul Taksim’de başlayan ve yurt sathına yayılan olaylar
When i access :
Ýstanbul Taksim’de baþlayan ve yurt sathýna yayýlan olaylar
Can you try to do this when creating the xmldocument
$dom = new DOMDocument('1.0', 'utf-8');
I am working with PHP and WordPress right now, I need to basically run the below code to Replace text in $current_path with the text in $new_path if $current_path EXIST in $content
I would prefer to be able to iterate over an array instead of running this over and over like this, or any better method would be nice?
$content = 'www.domain.com/news-tag/newstaghere'
$current_path = 'test-tag';
$new_path = 'test/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'news-tag';
$new_path = 'news/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'ppc-tag';
$new_path = 'ppc/tag';
$content = str_replace($current_path, $new_path, $content);
str_replace() accepts array arguments:
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag');
$new_content = str_replace($current_paths, $new_paths, $content);
Or you can use a single array with strtr():
$path_map = array('test-tag'=>'test/tag', 'news-tag'=>'news/tag', 'ppc-tag'=>'ppc/tag');
$new_content = strtr($content, $path_map);
However, you seem to be doing something very generic. Maybe all you need is a regex?
$new_content = preg_replace('/(test|news|ppc)-(tag)/u', '\1/\2', $content);
Or maybe even just
$new_content = preg_replace('/(\w+)-(tag)/u', '\1/\2', $content);
$content = 'www.domain.com/news-tag/newstaghere'
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag';
$content = str_replace($current_paths, $new_paths, $content);
Array arguments can be provided for the str_replace function, as noted on the following PHP.net page:
http://php.net/manual/en/function.str-replace.php
Please see "Example #2" on the page linked above for details.
You can do that:
$content = 'www.domain.com/news-tag/newstaghere';
$content = preg_replace('~www\.domain\.com/\w++\K-(?=tag/)~', '/', $content);
I have my code removing the <p> starting tags, but now I want to replace the ending </p> tags with line breaks. How can I do this?
This is what I have:
$content = 'This is the content';
$newcontent = preg_replace("/<p[^>]*?>", "", $content);
$newcontent = preg_replace("</p>", "<br />", $newcontent);
use str_replace instead of preg_replace, so:
$content = '<p>This is a new content for missing slash</p>';
$newcontent = preg_replace("/<p[^>]*?>/", "", $content);
$newcontent = str_replace("</p>", "<br />", $newcontent);
$content = preg_replace('#<p(.*?)>(.*?)</p>#is', '$2<br/>', $content);