Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/PHP/Modifying the content of your blog

March 14, 2014

Modifying the content of your blog

I wrote a custom blog in CodeIgniter two years ago. I revisited the code to change the content format a bit. I’ve noticed that there were several pieces of code that were commented out. At the time, I was playing around with what I could potentially do with the content. I thought I would share it here, because it will show you how you could drastically change the content of a blog by manipulating the content.

Let’s say the content of your blog is assigned to a variable called $item->content. We can alter the content by passing PHP functions to it. Let’s say we want to limit the content to just the first 330 characters. We can use a PHP function called substr. Substr returns a portion of the string from a specified start and length.

The Excerpt

$item->content = substr($item->content,0,330);
echo $item->content;

$item->content = substr($item->content,0,330); echo $item->content;

In this example, we are limiting content to the first 330 characters.

Remove page breaks and new lines

$item->content = str_replace(array('\r', '\n'), '', $item->content);
echo $item->content;

$item->content = str_replace(array('\r', '\n'), '', $item->content); echo $item->content;

In this example, str_replace replace page breaks and new lines with ” or nothing.

Remove two spaces

$item->content = str_replace('  ', '', $item->content);
echo $item->content;

$item->content = str_replace(' ', '', $item->content); echo $item->content;

Finally, adding Read More […]

$item->content = $item->content . '<a href=#>Read [...]</a>';
echo $item->content;

$item->content = $item->content . '<a href=#>Read [...]</a>'; echo $item->content;

I appended to content a link with the anchor of ‘Read […].’

Filed Under: PHP Tagged With: blog, codeigniter

Sign up

Get content delivered to your mail!

About Me

I'm Ulysses, a Cloud Engineer at Cardinal Health based in Columbus, Ohio. I’m a certified AWS Solutions Architect. This website is my way of documenting the things I've learned about the Cloud. When off the grid, I enjoy riding my electric skateboard. I've surfed, snowboarded and played saxophone in the past. I hope you find this site helpful. It's powered by WordPress and hosted in AWS.

  • Cloud
  • Linux
  • Git

Copyright © 2012–2021