Substring_index

I use the following tpo extract the first paragraph from a string:

SUBSTRING_INDEX(page_content,CHAR(13,10),1) AS first_paragraph

That works fine. However I need to extract the second paragraph as well but i can’t figure how to do so?

Any answer would be appreciated. Thanx

SUBSTRING_INDEX( SUBSTRING_INDEX(page_content,CHAR(13,10),2) page_content,CHAR(13,10),-1)) AS second_paragraph

Hi Rudy thanks for the reply. Should I call the first paragraph first_paragraph:

SUBSTRING_INDEX(
SUBSTRING_INDEX(page_content,CHAR(13,10),2) AS first_paragraph ???????????
                page_content,CHAR(13,10),-1)) AS second_paragraph

what happened when you tested that? ™

Just curious, what’s your use case? I see a lot of people treating the first paragraph as a kind of summary for texts but for a lot of texts that doesn’t make a whole lot of sense.
Just something to think about.

Hi Rudi. When I tested it like that I only got the secornd paragraph. So what am I doing wrong

Hi Scallio. It is just two paragraphs for the about us page, but I want to use them on two different sections on the page

no, it couldn’t possibly have worked, it would’ve produced a syntax error

you cannot put “AS something” inside the middle of a function’s arguments

in your SELECT clause, do the first paragraph the way you originally did, then add the expression for the second paragraph exactly the way i had it

MHi Rudi thanx again for the reaction. As soon as I add the second expression nothing is showing. This is the complete query:

SELECT `content_id`
     , `page_id` 
     , `page_naam`
     , `slider_title`
     , `page_title`
     , `page_content`
     , SUBSTRING_INDEX(page_content,CHAR(13,10),1) AS first_paragraph
     , SUBSTRING_INDEX(
       SUBSTRING_INDEX(page_content,CHAR(13,10), 2)
                       page_content,CHAR(13,10),-1)) AS second_paragraph
  FROM `page_content`

okay, i just tested it myself… i had a syntax error in my code!

i say again, your query did not execute

it did not show nothing, as you said – it didn’t run, and produced a syntax error

if you are testing your queries inside php, then your php error reporting facility is broken – you need to distinguish between an error and a successful execution that returns no rows

try this –

SELECT SUBSTRING_INDEX(page_content,CHAR(13,10),1) AS first_paragraph , SUBSTRING_INDEX( SUBSTRING_INDEX(page_content,CHAR(13,10),2) ,CHAR(13,10),-1) AS second_paragraph FROM ...

Hi Rudi. That works perfect. Thanx a lot :slight_smile:

Hi Rudi. I hope I can ask one more question about the subject. What if I need the third, fourth and fifth paragraph separated as well. How would I do that?

third, fourth, and fifth – same way as second, with a nested function call

SUBSTRING_INDEX( SUBSTRING_INDEX(page_content,CHAR(13,10),3) ,CHAR(13,10),-1) AS third_paragraph

the inner SUBSTRING_INDEX grabs everything up to the 3rd CRLF counting from the left

then the outer SUBSTRING_INDEX grabs everything from that, counting backwards to the first CRLF

Hi Rudi. This is what I have in the query:

, SUBSTRING_INDEX(page_content, CHAR(13,10), 1) AS firts_paragraph
, SUBSTRING_INDEX(
  SUBSTRING_INDEX(page_content, CHAR(13,10), 2), CHAR(13,10), -1) AS second_paragraph
, SUBSTRING_INDEX(
  SUBSTRING_INDEX(page_content, CHAR(13,10), 3), CHAR(13,10), -1) AS third_paragraph

Not sure what is wrong with this but when I echo firts_paragraph I don’t get any output, When I echo second_paragraph I get the first paragraph. When i echo third_paragraph I get the second and the third paragraph

Am I overseeing something?

maybe run it outside of php to confirm what you’re getting

then, if it still doesn’t make sense, go and look real hard at your data, especially the CRLF characters

When running this in myAdin I get the same results as descibed in above post. What should I change in the code.

Edit: This is the dat as it is in the database:

<p>U bent van plan een onvergetelijk barbecue party te organiseren voor uw collega’s, vrienden, familie of buren. Maar u heeft werkelijk geen idee waar u moet beginnen. BBQman neemt u graag het werk uit handen en verzorgt de gehele barbecue bij u op locatie: thuis of op uw bedrijf. We bieden verschillende barbecuepakketten samengesteld met verse vis-, vleesproducten, salades, sauzen, brood en nog veel meer!</p>

<p>Naast de verschillende barbecue pakketten regelen we ook alle benodigdheden voor een geslaagde BBQ. Als u bij BBQman uw barbecue bestelt hoef u als gastheer of gastvrouw nergens meer naar om te kijken en kunt u zich geheel aan uw bezoekers wijden. Dat is pas echt zorgeloos genieten! Dus waarom moeilijk doen als het makkelijk kan. Bestel uw barbecue bij de BBQman.</p>

<p>Thuis, bij u op de zaak of vereniging of welke locatie dan ook zo heeft u geen omkijken naar het de barbecue het vlees de sausen het brood en salades dus staat u geen uren in de keueken om het voor te bereiden en boodschappen te doen, we verzorgen een complete bbq, en u heeft voldoende tijd voor uw gasten zelfs de afwas nemen we weer mee.</p>

<p>Ons vlees en vis komt van door ons geselecteerde slagers en visboeren. Al ons vlees word door ons gemarineerd, de sauzen salades en kruidenboter, uit eigen keuken, worden met verse ingrediënten bereid. Het brood word kort voor gebruik afgebakken en is krakend vers. Al onze producten worden gekoeld vervoerd volgens de richtlijnen van de haccp en komen daardoor op een veilige en verantwoorde manier op uw bord terecht. Bij ons staat de kwaliteit en hygiëne hoog in het vaandel!</p>

<p>Door de samenstelling van onze barbecue pakketten is er voor elk budget een BBq van eenvoudig, tot super de luxe en dat voor een prima kwaliteit prijs verhouding zodat onze prijzen laag zijn te noemen. Ook is er de mogelijkheid een eigen pakket samen te stellen voor een prima prijs als u vragen heeft neem gerust contact met ons op.</p>

i would advise you to delimit on the actual paragraph ending tags, not a carriage-return-line-feed character, which can occur anywhere in html with no change in semantics, since it’s regarded as white space

you probably don’t even have any CRLF characters…

1 Like

No I don’t indeed

So based on how it is now what would be my options Rudi?

what do you think you should do?

also, how good are you at php (if that’s what you’re using)?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.