how to limit numwords article in codeigniter? - php

i have cms blog with codeigniter. but when i limit content in article is not working. i am newbie in codeigntier can you tell me what am i to do.
this is cms_helper.php
function get_excerpt($article, $numwords = 20){
$string = '';
$url = article_link($article);
$string .= '<h2>' . anchor($url, e($article->judul_berita)) . '</h2>';
$string .= '<p class="pubdate">' . e($article->tanggal) . '</p>';
$string .= '<p>' . e(limit_to_numwords(strip_tags($article->content), $numwords)) . '</p>';// content is not show
$string .= '<p>' . anchor($url, 'Read more', array('judul_berita' => e($article->judul_berita))) . '</p>';
return $string;
}
function limit_to_numwords($string, $numwords){
$excerpt = explode(' ', $string, $numwords + 1);
if (count($excerpt) >= $numwords) {
array_pop($excerpt);
}
$excerpt = implode(' ', $excerpt);
return $excerpt;
}
function e($string){
return htmlentities($string);
}
in controller
private function _homepage()
{
$this->load->model('mberita');
$this->db->limit(6);
$this->data['articles'] = $this->mberita->get_berita();
//var_dump($this->data['articles']);
}
please help me what to do. thank you.

You should use Codeigniter text helper:
$this->load->helper('text'); // add this to the the function or to the constructor
$string .= '<p>' . e(word_limiter(strip_tags($article->content), $numwords)) . '</p>';
In this article you can find many useful codeigniter text functions

Try the below sample. this will surely work.
function limit_to_numwords($text,$no_words){
$next=substr($text,$no_words,strlen($text));
$spacepos=strpos($next," ");
$desc=substr($text,0,$no_words+$spacepos)."...";
return $desc;
}
From this, you can get exact full word text from the nearest space character. hope this will help your requirement.
Also, try to call the function as "$this->limit_to_numwords"

Related

Cannot redeclare two functions

How can I solve the following issue with the code attached? Seems that somehow Wordpress (or some kind of plugin) is calling the function twice.
function my_wpcf7_form_elements($html) {
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
Fatal error: Cannot redeclare ov3rfly_replace_include_blank() (previously declared in /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21) in /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php on line 21
Dont nest the functions - your current code declares the inner function every time the outer function is called, thereby causing the error the second time:
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
Check this file for re declare function as error message suggested
/Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21
rename one function and see whether it working or not
Write a separate function,multiple function calling in nested function:
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $html;
}
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

How do I access a variable inside of preg_replace_callback?

I'm trying to replace {{key}} items in my $text with values from a passed array. but when I tried adding the print_r to see what was going on I got a Undefined variable: kvPairs error. How can I access my variable form within the preg_replace_callback?
public function replaceValues($kvPairs, $text) {
$text = preg_replace_callback(
'/(\{{)(.*?)(\}})/',
function ($match) {
$attr = trim($match[2]);
print_r($kvPairs[strtolower($attr)]);
if (isset($kvPairs[strtolower($attr)])) {
return "<span class='attr'>" . $kvPairs[strtolower($attr)] . "</span>";
} else {
return "<span class='attrUnknown'>" . $attr . "</span>";
}
},
$text
);
return $text;
}
Update:
I've tried the global scope thing, but it doesn't work either. I've added 2 print statements to see whats doing on, one inside and one outside the preg_replace_callback.
public function replaceValues($kvPairs, $text) {
$attrTest = 'date';
print_r("--" . strtolower($attrTest) . "--" . $kvPairs[strtolower($attrTest)] . "--\n");
$text = preg_replace_callback(
'/(\{{)(.*?)(\}})/',
function ($match) {
global $kvPairs;
$attr = trim($match[2]);
print_r("==" . strtolower($attr) . "==" . $kvPairs[strtolower($attr)] . "==\n");
if (isset($kvPairs[strtolower($attr)])) {
return "<span class='attr'>" . $kvPairs[strtolower($attr)] . "</span>";
} else {
return "<span class='attrUnknown'>" . $attr . "</span>";
}
},
$text
);
return $text;
}
The output I get is:
--date--1977-05-20--
==date====
As your callback function is a closure, you can pass extra arguments via use
function ($match) use ($kvPairs) {
...
}
better than polluting the global space

Implode a css image in php

Can anyone how to implode a image file in breadcrumb in php.
this is my code
function fold_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Use CSS to hide titile .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
// comment below line to hide current page to breadcrumb
$breadcrumb[] = drupal_get_title();
$count= sizeof($breadcrumb);
/* print $count; */
$primarytitle=$breadcrumb[$count-2];
/* print $test; */
$output .= '<nav class="breadcrumb">' . implode(' » ', $breadcrumb) . '</nav>';
return $output;
}
}
Here i need to implode a css image file instead of »
Something like this maybe?
$img_breadcrumb = '<img src="/path/img_bcrumb.png">';
then
$output .= '<nav class="breadcrumb">' . implode($img_breadcrumb, $breadcrumb) . '</nav>';
What about simple analogy:
implode(' <img src="path/to/yourimage.file" /> ', $breadcrumb)
Or better:
implode(' <span class="separator"></span> ', $breadcrumb)
and define CSS background to your .separator class.
This is simple:
implode('<span class="separator"><img src="path/to/imagefile.ext" alt="yourimage.file"/></span>', $breadcrumb);
Hope this helps :)

Problem returning excerpt with specified number of characters

I'm using the following code to grab a string and return a spesific number of characters
<div class="top_posts">
<ul>';
}
elseif($i>1 && $i<=$number_of_posts)
{
$retour.= "\n" . '<li>';
if($image_url) { $retour .= '<img src="' . get_bloginfo('template_directory') . '/js/timthumb.php?src=' . $image_url . '&h=120&w=180&zc=1" alt="" />'; }
$retour .= '<h6>' . the_title("","",false) . '</h6>';
$retour.= get_wpe_excerpt('wpe_popular_posts');
$retour.='</li>';
if($i%2==1)
$retour.= "\n" . '<li class="clear">&</li>';
}
$i++;
endforeach;
$retour.='</ul></div>';
return $retour;
}
add_shortcode('popular-posts', 'popular_posts_code');
The chunk at issue is this part
$retour.= get_wpe_excerpt('wpe_popular_posts');
which calls to
function wpe_popular_posts($length) {
return 55;
}
However I'm still getting the full text string untrimmed - any help appreciated.
//update
The get_wpe_excerpt function looks like this
function get_wpe_excerpt($length_callback='', $more_callback='') {
if(function_exists($length_callback)){
add_filter('excerpt_length', $length_callback);
}
if(function_exists($more_callback)){
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>'.$output.'</p>';
return $output;
}
Now sure how the get_wpe_excerpt() function is done. You may want to try the simple code I used in several WordPress themes.
implode(' ', array_slice(explode(' ', get_the_content()), 0, 55));
You want to return the first 55 characters of the string?
Try substr($input, 0, 55);
However I really don't understand what's going on with that call to $retour.= get_wpe_excerpt('wpe_popular_posts');... so maybe I am misunderstanding what you are trying to achieve?

Converting line breaks into <li> tags

I'm creating an upload form that has a text area for users to input cooking recipes with. Essentially, what I want to do is wrap each line in a <li> tag for output purposes. I've been trying to manipulate the nl2br function but to no avail. Can anyone help?
I'm retrieving the text area's content via POST and storing entries in a MySQL database.
Here's what the code looks like at the moment (the check_input function strips slashes, etc.):
$prepText=check_input($_POST['preparationText']);
$cookText=check_input($_POST['cookingText']);
Explode the string by \n then wrap each line in an li tag.
<?php
$string = "line 1\nline 2\nline3";
$bits = explode("\n", $string);
$newstring = "<ol>";
foreach($bits as $bit)
{
$newstring .= "<li>" . $bit . "</li>";
}
$newstring .= "</ol>";
Not quite pretty, but an idea that comes to mind is to :
explode the string, using newline as a separator
and implode the array, using </li><li> between items :
Which could be translated to something like this :
$new_string = '<li>' . implode('</li><li>', explode("\n", $old_string)) . '</li>';
(Yeah, bad idea -- don't do that, especially if the text is long)
Another solution, way cleaner, would be to just replace the newlines in your string by </li><li> :
(wrapping the resulting string inside <li> and </li>, to open/close those)
$new_string = '<li>' . str_replace("\n", '</li><li>', $old_string) . '</li>';
With that idea, for example, the following portion of code :
$old_string = <<<STR
this is
an example
of a
string
STR;
$new_string = '<li>' . str_replace("\n", '</li><li>', $old_string) . '</li>';
var_dump($new_string);
Would get you this kind of output :
string '<li>this is</li><li>an example</li><li>of a </li><li>string</li>' (length=64)
I created a function based on Richard's answer in case it saves anyone some time!
/**
* #param string $str - String containing line breaks
* #param string $tag - ul or ol
* #param string $class - classes to add if required
*/
function nl2list($str, $tag = 'ul', $class = '')
{
$bits = explode("\n", $str);
$class_string = $class ? ' class="' . $class . '"' : false;
$newstring = '<' . $tag . $class_string . '>';
foreach ($bits as $bit) {
$newstring .= "<li>" . $bit . "</li>";
}
return $newstring . '</' . $tag . '>';
}
function nl2li($str)
{
if (!isset($str)) return false;
$arr = explode("\r\n", $str);
$li = array_map(function($s){ return '<li>'.$s.'</li>'; }, $arr);
return '<ul>'.implode($li).'</ul>';
}
Input:
Line 1\r\nLine2\r\nLine3\r\n
Output:
<ul><li>Line 1</li><li>Line 2</li><li>Line 3</li></ul>
The simplest way to do it:
function ln2ul($string) {
return '<ul><li>' . str_replace("\n", '</li><li>', trim($string)) . '</li></ul>';
}

Categories