Support » Developing with WordPress » Text transformation in the_content()

  • Hello,
    there is a code to insert a “<br>” after writing three or four times “.” ?
    So that I transform the third or fourth “.” (Dot with space) into a “. <br>” (dot, and newline) inside the_content() code.
    Thank you for your work.

Viewing 6 replies - 16 through 21 (of 21 total)
  • Moderator bcworkz

    (@bcworkz)

    To fix the extra dot at the end issue, replace this line:
    $w_breaks = str_replace(['..', '<br>.',], ['.', '<br>',], $w_breaks );
    with this:
    $w_breaks = substr( $w_breaks, 0, '. '== $glue? -2: -5 );

    I think the vowel criteria can be accommodated, I want to be sure I understand correctly though. Do you only want to count dots that follow vowels when deciding to insert a break? Dots following consonants do not count? For example:
    Ciao. Come. Stai. Consonant. Non ti vedo. Da possibile. imbarazzante.
    Result:
    Ciao. Come. Stai. Consonant. Non ti vedo.
    Da possibile. imbarazzante.

    Is that what you mean?

    marcorroma

    (@marcorroma)

    Exact. I want to include all the vowels with the dot.
    To avoid wrapping when there are words like “Saint.” or “Sir.”

    Moderator bcworkz

    (@bcworkz)

    Hah, that could only work in Italian I think 🙂
    I restructured the foreach loop to accommodate that need. I then noticed my algorithm had problems with dots followed by HTML tags. I fixed it by inserting a space in between. I think the possibility of that insertion being a problem is very rare.

    The entire modified code:

    function replace_content($content)
    {
    // Standardize various system newlines as "\n"
    $content = str_replace(["\r\n", "\r",], "\n", $content );
    // Convert "\n\n" to <br><br> tags
    $content = str_replace( "\n\n", ' <br><br>', $content );
    // Replace remaining newlines with spaces
    $content = str_replace( "\n", ' ', $content );
    // Add spaces after dots followed by a HTML tag
    $content = str_replace( ".<", '. <', $content );
    $parts = explode( '. ', $content );
    $w_breaks = '';
    $count = 0;
    foreach ( $parts as $part ) {
      if ( in_array( mb_substr( $part, -1 ), ['a','e','i','o','u',])) {
        $glue = ( 3 == $count % 4 )?'.<br>':'. ';
        $count++;
      } else {
        $glue = '. ';
      }
      $w_breaks .= $part . $glue;
    }
    // fix trailing dots
    $w_breaks = substr( $w_breaks, 0, '. '== $glue? -2: -5 );
    
    return $w_breaks;
    }
    add_filter('the_content','replace_content');
    marcorroma

    (@marcorroma)

    Hi, sorry for the delay in my reply.
    The code is really fabulous to fix text problems of an archive too heavy to do it manually.

    Instead, if I want to fix the text and replace “</p><p>” once yes and once not. So the first time, the third time, the fifth time, the seventh time, etc …
    How can I do?

    Example:

    <p>Lorem Ipsum is simply dummy text.</p>
    <p>Of the printing and typesetting industry.</p>
    <p>Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s.</p>
    <p>When an unknown printer took a galley of type.</p>
    <p>Scrambled it to make a type specimen book.</p>
    <p>It has survived not only five centuries.</p>
    <p>But also the leap into electronic typesetting.</p>

    Final results:
    <p>Lorem Ipsum is simply dummy text. Of the printing and typesetting industry.</p>
    <p>Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s. When an unknown printer took a galley of type.</p>
    <p>Scrambled it to make a type specimen book. It has survived not only five centuries.</p>
    <p>But also the leap into electronic typesetting.</p>

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, but I’m not able to help you any further on this. I’ve more than demonstrated a basic algorithm that can be generally applied to any unique string, removing every nth occurrence. Similar logic can be applied to most situations. Every situation will likely need to be fine tuned with additional logic, depending on the specific string targeted.

    I’m not in a position to provide forum members with a free coding service for every minor need someone may come up with. To be clear, I’m not soliciting compensation, I’m not angry, you did nothing wrong, all’s fine. This was an interesting puzzle, but I’m done with it. I need to draw the line somewhere and this is it. I wish you the best of luck in finding solutions to your coding needs.

    Ok.
    I thank you for your work!

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘Text transformation in the_content()’ is closed to new replies.