Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

text format

Last post 07-02-2009, 6:08 AM by mirceasoaica. 4 replies.
Sort Posts: Previous Next
  •  07-01-2009, 5:34 AM 54466

    text format

    Hello.

    I have a text like this : 

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. {onpage} {    more}Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.{end} It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. {newpage}    {read more...}It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. {end}

    the resulting text should be this :

     <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<a href="something">   more</a><span class="someClass">Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</span><span> It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. <a href="something_else.html">    read more...</a></span>

    I think this could only be done by using regular expressions.

    i tried something but with no result. i'm not that good with regex.

    this is what i tried (in php) : 

    preg_match_all("|(.*)\{onpage\}\{(\[^}]*)\}(.*)\{end\}|i", $text, $match);

    this returned nothing.

    Filed under: , ,
  •  07-01-2009, 10:05 AM 54472 in reply to 54466

    Re: text format

    Please define the replacements that should occur (what keyword should be match and what replacement should occur for that match), they seem somewhat evident but there is variation in your { } blocks.
  •  07-01-2009, 11:08 AM 54475 in reply to 54472

    Re: text format

    My client wants to format a page using this "language".

    something like : 

    some text {onpage} {   more [...]} another text {end}

    {onpage} means that "Another text" will be hidden at first and when you press the link "   more [...]" that text will slide down. so this text should be transformed in something (not necesarry exactly) like :

     <span>some text <a href="something" class="someclass">   more [...]</a></span><span class="hidden">Another text</span>

     the same is with {newpage} except that the second {} parameter will be a link to a page wich will contain the "Another text" text.

     some text {newpage} {   more [...]} another text {end}

    will be :

     <span>some text <a href="some_page.php">   more [...]</a></span>

    I hope you can understand what i need.

    The "language" can be altered if this is too hard to do. 

    Can you help me with this?

    Thanks.

  •  07-01-2009, 4:00 PM 54479 in reply to 54475

    Re: text format

    <?php
    $text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. {onpage} {    more}Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.{end} It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. {newpage}    {read more... }It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. {end}";
    function repfunc($match){
            return $match[3]!='' ? '<span>'.$match[1].'<a href="something" class="someclass">'.$match[3].'</a></span><span class="hidden">'.$match[4].'</span>' : '<span>'.$match[1].'<a href="some_page.php">'.$match[6].'</a></span>' ;
    }
    $text=preg_replace_callback('/([^{}]*)(?:\{onpage\} *\{(([^}]*)\}(.*?))\{end\}|\{newpage\} *\{(([^}]*)\}(.*?))\{end\})/s','repfunc',$text);
    echo $text;
    ?>

    Note that $match[7] is defined in the match pattern but ignored in the replacement as in your example.  I left it defined in the event you choose to use it.


  •  07-02-2009, 6:08 AM 54501 in reply to 54479

    Re: text format

    Thanks alot man. it works.
View as RSS news feed in XML