Implode a css image in php - 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 :)

Related

how to limit numwords article in codeigniter?

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"

Print specific array value via function argument

$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $menu[$key] . '">' . $menu[$key] . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu[1]); ?>
What i basically want to do is to pass a specific array value when i'm echoing out the menu.
I'm building a single page website with anchors and i want to pass value's so i can echo out the "top"-link.
I'm stuck at the point on how to pass the $key value trough the function.
**edit: I'm trying to print specific links. I want a function that is able to print out an link but i want to specify the link to print via the function argument.
for example:
<?php echo main_menu($key = '0'); ?>
result:
prints url: top
<?php echo main_menu($key = '2'); ?>
result:
prints url: photography
**
(A lack of jargon makes it a bit harder to explain and even harder to google.
I got my books in front of me but this is taking a lot more time than it should.)
You either need to pass the entire array and loop, or pass a single array item and not loop:
Single Item:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
$return .= '<a class="menu" href="index.php#' . $menu . '">' . $menu . '</a>' . PHP_EOL .'';
$return .= '</div>';
return $return;
}
echo main_menu($menu[1]);
Entire Array:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach($menu as $value) {
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
echo main_menu($menu);
You don't need $menu[$key] just use the $value.
Should you not just be using $value inside your loop? And passing the entire array rather than one item of the $menu array?
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu); ?>
Try:
echo main_menu($menu); // You will get your links printed
Instead of
echo main_menu($menu[1]); // In this case error is occured like : **Invalid argument supplied for foreach**
NOTE: You can use $value instead of $menu[$key]

Breadcrumb displays query string

I would like my breadcrumb script to display the page name only(example: pic), instead of page name + query string if exists(example: pic).
This is my breadcrumb script:
<?php
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
echo '<li class="active">';
echo ucfirst(str_replace(array(".php","_",),array(""," "),$crumb) . ' ');
echo '</li>';
}
?>
How do I exclude everything after ".php"?
Thank you for your help
use parse_url() function for getting the parts of the url
You may use this after some editing
<?php
$parsed=parse_url($_SERVER["REQUEST_URI"]);
$crumbs = explode("/",$parsed['path']);
print_r(parse_url($_SERVER["REQUEST_URI"]));
foreach($crumbs as $crumb){
echo '<li class="active">';
echo ucfirst(str_replace(array(".php","_",),array(""," "),$crumb) . ' ');
echo '</li>';
}
?>
<?php
$crumbs = explode("/",$_SERVER["PHP_SELF"]);
$html="";
foreach($crumbs as $crumb){
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb));
if(trim($crumb)!=""){
$html .= '<li class="active">'.$crumb.'</li>';
}
}
$html = "<li>".ucfirst($_SERVER["HTTP_HOST"])."</li>".$html;
echo $html;
?>

Need to create li with list of different links using php explode method

here is my working php explode code for NON links:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li>' . $item . '</li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
..and the code which defines "my_custom_output", which I input into my textarea field:
text1,text2,text3,etc
..and the finished product:
text1
text2
text3
etc
So that works.
Now what I want to do is make text1 be a link to mywebsite.com/text1-page-url/
I was able to get this far:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li class="link-class"><a title="' . $item . '" href="http://mywebsite.com/' . $item_links . '">' . $item . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
Now, I would like $item_links to define just the rest of the url. For example:
I want to put input this into my textarea:
text1:text1-page-url,text2:new-text2-page,text3:different-page-text3
and have the output be this:
text1
text2
..etc (stackoverflow forced me to only have two links in this post because I only have 0 reputation)
another thing I want to do is change commas to new lines. I know the code for a new line is \n but I do not know how to swap it out. That way I can put this:
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
I hope I made this easy for you to understand. I am almost there, I am just stuck. Please help. Thank you!
Just split the $item inside your loop with explode():
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And choose your string separators so $item_text, $item_links wouldn't contain them.
After you explode the string you could loop through again and separate the text and link into key/values. Something like...
$array_final = new array();
$array_temp = explode(',', $textarea);
foreach($array_temp as $item) {
list($text, $link) = explode(':', $item);
$array_final[$text] = $link;
}
foreach($array_final as $key => $value) {
echo '<li>'.$key.'</li>';
}
to change comma into new line you can use str_replace like
str_replace(",", "\r\n", $output)

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?

Categories