Jump to content

Creating An Article Caption From MySQL Data


CStrauss

Recommended Posts

Im creating an adminstration part of my site where I can ad new articles to my site by typing in a form my article and what Im looking for is a way to pull out the first paragraph of an article to display on my home page as a caption to the article then a link to read more.For this to work it has to be able to store html in the database i.e. the <p> so it knows how to parse out the first paragraph of that paragraph and store it in a varible to use. So if anyone knows of any online tutorials that cover these types of topics thats what im looking for at the momemnt so i can read up how it should be done and what not. Im sure there something out there that I just havent found on google yet. So if you know of one please post back a link for me.Thank you.

Link to comment
Share on other sites

the way that i did it was to explode the article on a new line, like this:$para = explode("\n", $row['article'])...then i just ran this:echo $para[0];...but if you want to create an article excerpt, you might be better off exploding at the space (' ') and echoing the first 30 words. that way, your entries won't be lopsided when articleX has a ten-word first paragraph and articleY has a 100-word first paragraph......i did the latter on a project, and you can see how it looks here......hope that helps!love,jason

Link to comment
Share on other sites

Okay I solved the problem of creating a caption for my articles. Basicly here is what I did: I first use the nl2br() function to create <br> tags between paragarphs.next I used strpos() to find the first <br> tagThen I stored that in a variable called caption concatentated between to <p> tags and there i got my caption to insert into my database using substr() with the varaible from the strpos as the length parameter.However I came across one problem to achieve my goal. I actually want to insert the article with <p> tags to seperate paragraphs rather then <br /> tags. This will make it easer to format with my css style sheets. Now i have a functions called br2nl i could convert <br> tags back to new line breaks. Im just not sure what the best string function to take the paragraphs between the nl breaks and put them between <p> tags.I notice when I use the strpos() to find the <br> it only return the position of the first <br> tag. So im thinking i could run it through a loop with the same concept i use before using strpos() variale as the starting point of the new paragraph of my article. Now if this will work I just need to find away to insert <p> tags at the beging and end of each paragraph. Would the str_replace() function be good for this since im not actually replacing any string in actuallity but just adding to a current string?Any other ideas or flaws in my theory you might see let me know. Keep in mind Im not good with regexps yet(still learning and have a few question to post on that topic another day). Thats all for now would appreciate some feed back on my theory?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...