How can I disable RSS feed description after 2 sentences using preg_split?

I want to describe an RSS feed located in $ the_content and disable it after 2 full sentences (or 200 words and then the next full sentence) using preg_split.

I tried a couple of times but I quit. I know what I want to do, but I can't even begin to do something for this job.

Thank!

+1


source to share


1 answer


Partitioning HTML correctly is very difficult and shouldn't be done with regular expressions. If you want HTML, something like a DOM text iterator will be helpful .

  • Converting description to text:

    $text = html_entities_decode(strip_tags($html),ENT_QUOTES,'UTF-8');
    
          

  • This takes the first 200 characters (200 words is too much for a sentence, isn't it?) And then look for the end of the sentence:

    $text = preg_replace('/^(.{200}.*?[.!?]).*$/','\1',$text);
    
          

You can change [.!?]

to something more complex, for example. require space after punctuation or require no punctuation nearby:



  (?<![^.!?]{5})[.!?](?=[^.!?]{5})

      

(?=…)

is a positive statement. (?<!…)

negative assertion that looks beyond the current position. {5}

means 5 times.

I have not tested it :)

+1


source







All Articles