Jump to content
Fran

First Paragraph Limiter

Recommended Posts

Is there any way to limit the text just to the first paragraph of the body lets say? Thanks very much guys. 

Share this post


Link to post
Share on other sites

Thanks Diogo

I tried this:

<?php
      $lotes = $pages->get(1025)->children();
      function getFirstPara($string) {
      $string = substr($string,0, strpos($string, "</p>")+4);
      return $string;
      }
      foreach ($lotes as $lote) {
      echo getFirstPara($lote->body);
      }
?>

And it worked great !!! :D

  • Like 1

Share this post


Link to post
Share on other sites

Great!

You might want to move that function to a file that is called by all the others, so you have it available averywhere, like _func.php or even the head file if you prefer.

  • Like 1

Share this post


Link to post
Share on other sites

In the latest 3.x version, there's also some nice additions that will let you easily do this.

http://processwire.com/blog/posts/processwire-3.0.7-expands-field-rendering-page-path-history-and-more/#render-groups

Nice. Funny that there is a regex example that seems to do what ts wants.

<?php

// render the first found paragraph only

if(preg_match('!(<p>.+?</p>)!', $value, $matches)) {

echo $matches[1];

}

Regex is basically voodoo to me so i don't quite understand the exclamation marks but i'm sure Ryan has a better grasp :)

Share this post


Link to post
Share on other sites

The exclamation marks are simply pattern delimiters. Normally, this is "/", but PHP looks for the first character in a pattern string and treats it as the delimiter. This comes in handy whenever you have a pattern containing a forward slash, as you'd otherwise have to escape it to tell PHP that it's part of the pattern and not its end.

  • Like 1

Share this post


Link to post
Share on other sites

As a follow up question to the first paragraphy solutions provided. Is there a way to get "the rest of the text" output? A way to exclude the first paragraph?

The result I am looking for is to output the first paragraph from a body text field, then an image from an image field, and then the rest of the text from the body text field. So there might be other solutions?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Raymond Geerts
      I was wondering how to achieve the following.
      A total of 100 pages which are listed with a limit of 10 items per page, resulting in 10 pages of 10 items.
      Instead i desire the following (below), and wondering how to setup the code / selector(s).
      The first page should show a list of 8 items. After that all following pages should have a limit of 10 items.
      page 1 -> items 1 - 8 -> (limit 8) page 2 -> items 9 - 18 -> (limit 10) page 3 -> items 19 - 28 -> (limit 10) ... page 9 -> items 79 - 88 -> (limit 10) page 10 -> items 89 - 98 -> (limit 10) page 11 -> items 98 - 100 -> (limit 10) The ideal situation would be to have an extra page selector property aside of 'limit', something named 'limit_first' or alike.
    • By a-ok
      Hi folks,
      I'm set up a simple next/prev between pages that are loaded in via pjax with the following:
      <?php if ($page->next->id) : ?> <div class="next-solution-container">      <a data-pjax class="next-solution" href="<?php echo $page->next->url; ?>"><span><?php echo $page->next->title; ?></span></a> </div> <?php endif; ?> This worked great, but I want it now to loop... so if it's the last item, it gets the first item as the next link. I thought the following below would work, but as I am using it on the page template, and thus using $page, I don't think first/last are in use?
      <?php if ($page == $page->last) : ?> <div class="next-solution-container">      <a data-pjax class="next-solution" href="<?php echo $page->first->url; ?>"><span><?php echo $page->first->title; ?></span></a>     </div> <?php else : ?> <div class="next-solution-container">      <a data-pjax class="next-solution" href="<?php echo $page->next->url; ?>"><span><?php echo $page->next->title; ?></span></a>     </div> <?php endif; ?> Any thoughts?
    • By enricob
      Hi all,
      I am building a page that contains a quite complex request form.
      To do this, I created a page based on a "contact-form" template, and inside this template I put all the code needed to display the form and manage the results.
      The form is built using PW API, the code is inspired from some examples I found here on the forum.
      My problem is that I don't know how to insert a text paragraph between the fields, anyone has idea on how to do this?
      Here's a very simplified version of my code (only two fields displayed), in this case I would like to append a custom paragraph (simple html) between Company and Name fields.
      The only solution I found is to use an additional field of Fieldset type, without content, and on the label I set the text I need to output. However in the label field I am not able to insert html tags...
      Any help is very appreciated... Thank you!
      // create a text input $field = $modules->get("InputfieldText"); $field->label = "Company"; $field->attr('id+name','company'); $field->required = 1; $form_fields[] = $field; // create a text input $field = $modules->get("InputfieldText"); $field->label = "Name"; $field->attr('id+name','name'); $field->required = 1; $form_fields[] = $field; // submit button $submit = $modules->get("InputfieldSubmit"); $submit->attr("value","Send"); $submit->attr("id+name","submit2"); $submit->skipLabel = '4'; // create the form field (also field wrapper) $form = $modules->get("InputfieldForm"); $form->action = "./"; $form->method = "post"; $form->attr("id+name",'contact-form'); foreach ($form_fields as $field) { $form->append($field); } $form->append($submit); // append the form in $out variable $out .= $form->render();
    • By gRegor
      Hello,
      I have built a form that allows authenticated members to submit articles through a front-end form. I was under the impression that 2 newlines would automatically be converted to paragraph elements, but I'm pretty sure that's incorrect as I re-acquaint myself with this aspect of PW.**

      Then I was thinking there was a TinyMCE configuration to convert newlines to paragraphs. There is, but I'm pretty sure that applies only during entry into the textarea, not when the text data is first loaded into the TinyMCE textarea.
      First, is there a built-in PW paragraph formatting function that I'm missing/forgetting?
      If not, would the "preferred" method to achieve this be using TinyMCE on the front-end form, so newlines are converted to paragraphs before the form is submitted and the page fields are stored? If so, can someone point me to an example of using the PW API to include a TinyMCE field input?

      Or would the preferred method be to process the raw input to convert newlines to paragraphs (through another module, or custom code) before saving the page fields?

      I'm aware there is an Auto Paragraph TextFormatter module, but I believe that only applies when the data is output — not when the data is shown in backend.

      Thanks for any assistance!

      ** I have confirmed that the newlines are stored in the database, so I've confirmed they're neither being removed nor converted.
    • By photoman355
      I've been trying to make a carousel with a repeater field that has an "active" class on the first div.  I found a similar thread here but still can't get it to work.  Most likely me having a dumb moment
      Here's my code:
      <div class="carousel slide <?=$page->position?>"> <h4><?=$page->heading?></h4> <!-- Carousel items --> <div class="carousel-inner"> <?php $output = ''; $class = 'item'; foreach($page->testimonials as $testimonial) { $status = ''; if ($testimonial === $page->testimonials->first()) $status .= ' active'; $class .= $status; $output .= "<div class='{$class}'> <img class='quotes' src='{$config->urls->templates}assets/img/quotes.png'> <p>{$testimonial->body}</p> <h6>{$testimonial->person_name}</h6> </div>"; } echo $output; ?> </div> </div>   Any ideas where I'm going wrong?  
×
×
  • Create New...