0

I am going to add a simple blog to my new site, now I am going to integrate CKEditor to make it easier for users to enter in their post.

But I am like most blogs going to have a page of recent posts, that will display an excerpt of the post and you can click on whichever you want. However if a post has an image within the first 250 chars of the entry then I can see it is going to break the summary.

So my question is whether I can strip out the image from the excerpt to prevent this? Images will either be links externally or stored on the site rather than in the database as I expect it's best keeping images out of there for performance.

Once they click on the post they want the single page will put all the images in, but I am just struggling (probably because I am thinking rather than doing) to figure how I would do that.

nietonfir
  • 4,797
  • 6
  • 31
  • 43
Holo
  • 579
  • 10
  • 26
  • I suppose you need to strip_tags from message and you will get only textual part of it, then you will be able to trim, substr and present only part of it – Iłya Bursov Oct 22 '13 at 22:12
  • Personally I wouldn't do this in MySQL but rather in PHP before the data ends up in MySQL – Jason Sperske Oct 22 '13 at 22:12

2 Answers2

1

If I understand correctly, my way to go would be to use the strip_tags() function and allowing whatever tags you might want to stay active. For example, if you wanted to strip a string from it's images, but keep the bold, italic, links and line breaks, you could use a line like this:

echo strip_tags($yourString,"<b><i><a><br>");

But there are a lot of other ways to go about doing this, and a preg_replace, as submitted by Lajos is just as good. It really all depends on what exactly you want to do.

I personally really like the strip_tags, since it allows for really easy changes.

Émile Perron
  • 813
  • 10
  • 26
0

Maybe something like this?

$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 

Source: PHP - remove <img> tag from string

Community
  • 1
  • 1
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56