We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21946
    • 283 Posts
    Hello !
    I’m pretty bad with regex so if someone could check my code to say if i’m using the right functions and regex... The snippet works but maybe could work fastly and better ;-)

    I’m creating a little snippet wich divide the "content field" in several parts like this :

    ==iLoveFlowers==

    flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers

    ==cornFlakes==

    corn flakes corn flakes corn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakescorn flakes corn flakes

    my snippet’s call is
    [!Content? &partie=`iLoveFowers`!]
    and he print only :

    flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers flowers


    This is the code of the snippet


     <?php
    $partie = isset($partie)? $partie:0; 
    $contenu= $modx->documentObject['content']; //get the content of the document
    preg_match_all('#==\s*([a-zA-Z0-9_-]*)\s*==#',$contenu,$matches); //find the  "==titles==" and put them in a array "matches"
    $decoupes=preg_replace("#==\s*[a-zA-Z0-9_-]*\s*==#", "||", $contenu); //replace the ==titles== by ||
    $decoupes=explode("||", $decoupes); //now i can explode in a array to cut m content in several parts
    array_shift($decoupes); // firts entry is always empty in this array, i delete it
    $tab=array_combine($matches[1],$decoupes); //i create a array looking like "iLoveFlowers=>text aboutflowers, etc..."
    return $tab["$partie"];
    
    ?>
      [url=http://www.savepoint.fr/index.php?id=38] -petits tuyaux pour les d
      • 18940
      • 152 Posts
      I’m sorry if I’m getting this all wrong but isn’t it easier to make two chunks:

      {{iLoveFlowers}}
      FLOWERS FLOWERS FLOWERS FLOWERS FLOWERS FLOWERS etc

      {{cornFlakes}}
      CORNFLAKES CORNFLAKES CORNFLAKES CORNFLAKES etc

      I believe their is a way to embed two pages into one as well (search it wink ).

      Good luck
        Quality doesn&#39;t need a big signature.
        • 21946
        • 283 Posts
        Yes it is; and create a template variable is not a bad solution either; but the first idea isto allow the user to cut the text of the content in tinyMCE where he wants. It is useful for me because i need to put the [*content*] in two columns(two <div>)

        example of [*content*] :
        blablablablabla
        {cut}
        blilbilbiblibliblblib


        I can achieve to print only bliblibli or only blablablabla with this call
        <div id="first">[!content? &part=`1`!]</div>
        <div id="second">[!content? &part=`2`!]</div>

        <?php
        $part = isset($part)? $part:0; 
        $separator='{cut}'; //choose the separator you want to put in TinyMCE to cut the content
        $contenu= $modx->documentObject['content']; /
        $tab=explode($separator,$contenu); /
        $compteur=count($tab); 
        if ($part>$compteur){$output="<br/>This part doesn't exist...<br/>";} 
        for ($i = 0; $i < $compteur; $i++)
        {
        	if($part==$i+1){$output=$tab[$i];}
        }
        return $output;
        ?>


        The snippet i talk about in the first post is work in the same way except it is possible to give name to the parts and that content is cutted automatically with the "==title=="

          [url=http://www.savepoint.fr/index.php?id=38] -petits tuyaux pour les d
          • 18940
          • 152 Posts
          I get it now, nice thinking. As I’m a total PHP n00b I can’t help you futher so that were my two cents grin

          Good luck wink
            Quality doesn&#39;t need a big signature.
            • 25483
            • 741 Posts
            Wouldn’t it be easier for the client to create a extra template variable with a rich text editor?
              with regards,

              Ronald Lokers
              'Front-end developer' @ h2o Media

              • 21946
              • 283 Posts
              yes you’re right : creating a template variable would work also and in most of cases it is the best solution.
              In my design, dividing the content with {cut} is a pretty cool way to split our content between two different<div> (just for the home page) without share it between two fields.
              The code of my first post is useless for the moment and it’s a theorical question with an example. smiley
                [url=http://www.savepoint.fr/index.php?id=38] -petits tuyaux pour les d