We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12410
    • 353 Posts
    Hi Guys,
    How do I convert the below so that it works as a snippet in Revo?

    			
    <?php
    $Sentence = "[[*pagetitle]]";
    $Words = explode(" ", $Sentence);
    $WordCount = count($Words);
    $NewSentence = '';
    for ($i = 0; $i < $WordCount; ++$i) {
        if ($i < 1) {
            $NewSentence .= '<strong>' . $Words[$i] . '</strong> ';
        } else {
            $NewSentence .= $Words[$i] . ' ';
        }
    }
    echo $NewSentence;
    


    Its supposed to bold the first word in a sentence but when used in a Revo template it bolds all words. If I use it standalone as boldword.php it works fine. Not quite sure how to adjust the code to fulfill these requirements: http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets#Snippets-ReadingValuesinyourSnippets
    Using return instead of echo also doesn't work.

    Thanks

    PHP Version 5.5.9-1
    Ubuntu 14.04 LTS
    Apache 2
    MODX Revolution 2.2.14-pl (traditional)
    MySQL 5.5.3.7 [ed. note: howster last edited this post 9 years, 10 months ago.]
    • You need to use $modx->resource->get('pagetitle') in a snippet's code rather than the MODX tag.

      http://bobsguides.com/revolution-objects.html
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        Here's a slightly different way:

        <?php
        $sentence = $modx->resource->get('pagetitle');
        $words = explode(" ", $sentence);
        $newSentence = '<strong> ';
        $strongClosed = false;
        foreach ($words as $word) {
            $newSentence .=  $word . ' ';
            if (! $strongClosed) {
                $newSentence .=  . '</strong> ';
                $strongClosed = true;
            } 
        }
        echo $newSentence;


        I'm assuming that you don't really need a wordcount and I changed the variable names to conform with the typical PHP convention of having variable names start with a lowercase letter. My apologies if you use a different scheme.

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 12410
          • 353 Posts
          Thanks guys but unfortunately that bolded all words and not just the first. I need my output to be

          The Black Cat [ed. note: howster last edited this post 9 years, 10 months ago.]
          • Just to throw an alternative in, you could use a regular expression:
            <?php
            $sentence = $modx->resource->get('pagetitle');
            $sentence = preg_replace('/^([^\b].*?\b)/', '<strong>$1</strong>', $sentence);
            echo $sentence;
            [ed. note: garryn last edited this post 9 years, 10 months ago.]
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 3749
              • 24,544 Posts
              I suspect that the words in your pagetitles are separated by something other than spaces, so the code is treating the whole pagetitle as a word.

              Try this:

              $sentence = $modx->resource->get('pagetitle');
              $words = explode(" ", $sentence);
              $output = '<pre>' . print_r($words, true), '</pre>';
              return $output;

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting