• Resolved petermae

    (@petermae)


    I need to extract the first line of the content of my posts with php. I also need to extract a number of characters fron the rest of the post.

    E.g. my post content structure looks like this:

    https://google.com/or-some-other-random-link

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus quam at est lobortis, id euismod augue venenatis. Fusce quam nisl, aliquam vitae porta vel, dapibus ut augue. Etiam ultricies sem sed lacinia adipiscing. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

    I’m not very well travelled in PHP, so I’m a bit at a loss. Should I do this with a loop of some sort? One that will end when it meets the
    code? Or is there a better way?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Depends how you define a “line”. A loop might be appropriate, for instance you can explode() the string into an array of words and then rebuild the string until a “word” with a dot on the end is found. But how to deal with possible dots for other reasons, like numbers and abbreviations? Or lines that end with other punctuation. Or is a “line” terminated with a line feed and/or carriage return character?

    You may be able to use a regular expression and preg_match() to identify a complete line without needing to loop through every word. To arbitrarily extract a number of characters, substr() would suffice. PHP has a rich set of string manipulation functions. If you are creative, you should certainly find a combination that works well for you.

    Have you tried a generic search? You may find a predefined algorithm that does this quite well without having to reinvent the wheel.

    Excluding the initial link should be a matter of starting your line search only after the occurrence of the “” substring in the content. And perhaps “
    ” and “</p>” and other whitespace variants.

    Thread Starter petermae

    (@petermae)

    Thanks for the reply.

    I used the explode function as you suggested after I managed to pop in a random text string to identify where I needed to split (explode?) the content.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Parsing only first line of posts with php (before a )’ is closed to new replies.