Wordpress: How to split Widget title like Post and title - php

Hi i am working on wordpress . I want to split my Widget's Title like i did in Post-title(Given below).
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php
global $post;
$str = $post->post_title;
$exp = explode(" ",$str);
echo "<h2>".$exp[0];
echo " <span> ".$exp[1].' '.$exp[2].' '.$exp[3].' '.$exp[4].' '.$exp[5].' '.$exp[6]."</span></h2>";?></a>
Output is:
<h1>This<span> is a title</span></h1>

This will might help you.
global $post;
$arr = explode(' ',$post->post_title);
$j=0;
$str = '';
for($i=0; $i<count($arr); $i++){
$j = $j + 1;
if($j == 1){
$str .= $arr[$i].' <span>';
} else{
$str .= ' '.$arr[$i];
}
}
$str .= '</span>';
echo $str;

Related

Use Array in PHP [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 2 years ago.
I'm fairly new to PHP and I'm trying to make an array and use the elements of that array in my PHP Website by using $_GET.
I get my array like this:
<?php $values = explode(",", $_GET["id"]); ?>
And these here work fine:
<img src="<?php echo $values[0]; ?>">
<img src="<?php echo $values[1]; ?>">
This one however doesn't work
$newsSource = array(
array(
"title" => "COMPANY",
"url" => $values[2]
)
);
...
function getFeed($url){
...
Can I not assign an array value like that?
URL Example
https://DOMAIN/PHPFILE.php?id=IMAGE.jpg,IMAGE.png,RSS_FEED_WEBSITE,one
Full Code Here:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php $values = explode(",", $_GET["id"]);
// 0 = Logo
// 1 = News Logo
// 2 = RSS Feed
// 3 = Page
?>
<div class="container">
<div class="logo">
<img src="<?php echo $values[0]; ?>">
</div>
<div class="logo">
<img src="<?php echo $values[1]; ?>">
</div>
<?php
function getContent() {
$file = "./feed-cache.txt";
$current_time = time();
$file_time = filemtime($file);
if(file_exists($file) && ($current_time < $file_time)) {
return file_get_contents($file);
}
else {
$content = getFreshContent();
file_put_contents($file, $content);
return $content;
}
}
function getFreshContent() {
$html = "";
$newsSource = array(
array(
"title" => "COMPANY",
"url" => $values[2]
)
);
function getFeed($url){
$html = "";
$rss = simplexml_load_file($url);
$count = 0;
$page = $values[3] ?? 'one';
if ($page == 'two') {
$counter = 2;
} else if ($page == 'three') {
$counter = 4;
} else {
$counter = 0;
}
$itemcount = count($rss->channel->item);
for ($i = $counter; $i <= $counter+1; $i++) {
$curr_item = ($rss->channel->item[$i]);
// split description from img and text
$content = ($curr_item->description);
preg_match('/(<img[^>]+>)/i', $content, $matches);
$item->image = $matches[0];
$item->description = str_replace($matches[0],"",$content);
$html .= '<div class="entry"><h3>'.htmlspecialchars($curr_item->title).'</h3>';
$html .= '<div class="entry-image">'.($item->image).'</div>';
$html .= '<div class="entry-text">'.($item->description).'</div>';
$html .= '</div>';
}
return $html;
}
foreach($newsSource as $source) {
//$html .= '<h2>'.$source["title"].'</h2>';
$html .= getFeed($source["url"]);
}
return $html;
}
print getContent();
?>
</div>
</body>
</html>
After updating the problem, I found the problem.
In methods, variables need to be passed in!
function getFreshContent($values) {
// ...

Split array into div [duplicate]

This question already has answers here:
Break PHP array into 3 columns
(7 answers)
Closed 7 years ago.
I have this array :
$result = array('description1', 'description2', 'description3', 'description4', 'description5'
I want to split this array into divs like this :
$result[0] - $result[1] => put these into a div
$result[2] - $result[3] => put these into a div
$result[4] => put this into a div
My entire structure
$content = get_the_content();
$description = array();
$j=0;
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
for( $i = 0; $i < count($match[0]); $i = $i+1 ) {
$description[] = $match[0][$i];
}
}
$attachments =& get_children($args);
$arrayMatches = array();
if ($attachments) {
foreach(array_chunk($attachments, 2) as $img) {
echo '<div class="two_cols">';
foreach($img as $attachment) {
foreach($attachment as $attachment_key => $attachment_value) {
$imageID = $attachment->ID;
$imageTitle = $attachment->post_title;
$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
$imageURI = $imagearray[0]; // 0 is the URI
$imageWidth = $imagearray[1]; // 1 is the width
$imageHeight = $imagearray[2]; // 2 is the height
?>
<div class="col_1_2">
<!-- A picure Here -->
<?php $arrayMatches[] = $match[0][$j]; ?>
</div>
<?php
break;
}
$j++;
}
$arrayMatches = array_chunk($arrayMatches, 2);
echo "<div>";
foreach($arrayMatches as $v) {
echo implode($v);
}
echo "</div>";
echo '</div>';
}
}
This should work for you:
Just chunk your array with array_chunk(). And then you can simply loop through your array, output it into a div and implode() the elements.
<?php
$result = array('description1', 'description2', 'description3', 'description4', 'description5');
$result = array_chunk($result, 2);
foreach($result as $v) {
echo "<div>" . implode(" ", $v) . "</div>";
}
?>
output:
<div>description1 description2</div>
<div>description3 description4</div>
<div>description5</div>
EDIT:
As from your updated array structure just grab all values first like this:
$result = [];
$arr = array(['description1'], ['description2'], 'description3', 'description4', 'description5'); //example
array_walk_recursive($arr, function($v, $k)use(&$result){
$result[] = $v;
});
$result = array_chunk($result, 2);
Can you not just echo them in divs:
<div>
<?php echo $result[0] . "-" . $result[1]; ?>
</div>
<div>
<?php echo $result[2] . "-" . $result[3]; ?>
</div>
<div>
<?php echo $result[4]; ?>
</div>
You just need to control whether you are in the last 2 positions or not.
echo '<div>';
for ($i=0;$i<count($result);$i=$i+2) {
if ($i+1 >= count($result)) {
echo $result[$i];
} else {
echo $result[$i].$result[$i+1];
echo '</div><div>';
}
}
echo '</div>;

Displaying Blogs according to publish time

I have a doubt am displaying posts of blogs[more than 1] and now i want to display blogs according to publish date mean new post 1st next 2nd and so on...
MY CODE
require_once('rss_fetch.inc');
$dateArray= "";
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
'http://raghuks.wordpress.com/feed'
);
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
$dateArray=array();
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 1) break;
}
}
Only recent 4 posts should display from any blog but that should be according to date
Please help..
What i tried is putting all date into an array and using bubble sort but its not working.. Please Help Me..
Thanks In Advance
require_once('rss_fetch.inc');
$dateArray= "";
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
'http://raghuks.wordpress.com/feed'
);
$result_array = array();
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
$dateArray=array();
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
$tm = strtotime($date);
$result_array[$tm]['title'] = $title;
$result_array[$tm]['url'] = $url;
$result_array[$tm]['desc'] = $desc;
$result_array[$tm]['date'] = $date;
if($i == 1) break;
}
ksort($result_array);
foreach($result_array as $result)
{
echo "<div class=\"blog\"><a target=\"_blank\" href=$result['url']><h1>$result['title']</h1>$result['desc']<br/><br/>DATED : $result['date'] <br/><br/></a></div> ";
}
}

About displaying posts from two or more blogs

I want to display posts of two or more blogs in my website now am using magpierss-0.72 for fetching the posts and my code is
require_once('rss_fetch.inc');
$url = 'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss'
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j) {
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 3) break;
}
Here i can specify only one url of feeds and can fetch but now i want to display posts of two or more blogs Please give me the solution
Thanks in advance
Just use an array and throw in another foreach:
<?php
require_once('rss_fetch.inc');
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
' more urls ... ',
);
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 3) break;
}
}

PHP format split number into separate html tags

How would I go and split number 12345 into something like this with PHP:
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
echo preg_replace('((.))', "<span>$1</span>\n", '12345');
Try with:
$input = 12345;
foreach ( str_split($input) as $char ) {
echo '<span>' . $char . '</span>';
}
With str_split()
<?php
$str="12345";
$str = str_split($str);
foreach ($str as $letter){
echo '<span>'.$letter.'</span>'.PHP_EOL;
}
?>
$str = '12345';
$arr = str_split($str);
foreach ($arr as $char) {
echo '<span>' . $char . '</span>';
}
var arr = explode('', "12345");
Then iterate throgh it with foreach and echo (or do whatever you want with) the tags.
If the input string will be large, something like this will be more memory-friendly and possibly faster because it streams instead of breaking it apart first.
$string = "12345";
for ($i = 0, $j = strlen($string); $i < $j; $i++) {
echo "<span>" . $string{$i} . "</span>\n";
}

Categories