Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Parse / Split String Question

    I'm trying to parse/split the following text using preg_replace and haven't had too much luck so far ... if anyone could shed some light on how to get the results I would really appreciate it.

    Code:
    String =
    
    string is surrounded by other text and also 
    contains the brackets exactly as shown ... [[please parse|this part]] 
    
    I would like to do something like this with it:
    
    <a href="http://www.domain.com/please parse">this part</a>
    Thanks ~

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Here you go .....

    PHP Code:
    <?php

    $text
    ="string is surrounded by other text and also contains the brackets exactly as shown ... [[please_parse|this part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[also_parse|this second part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[another_part|to parse]] 
    "
    ;

    function 
    url_links($string)
    {
    $string preg_replace('/\[\[(.*?)\|(.*?)\]\]/i''<a href="http://www.mysite.com/$1" target="_blank">$2</a>'$string);
    return 
    $string;
    }


    print 
    url_links($text);

    ?>
    Last edited by mlseim; 03-14-2007 at 02:29 PM.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by mlseim View Post
    Here you go .....

    PHP Code:
    <?php

    $text
    ="string is surrounded by other text and also contains the brackets exactly as shown ... [[please_parse|this part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[also_parse|this second part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[another_part|to parse]] 
    "
    ;

    function 
    url_links($string)
    {
    $string preg_replace('/\[\[(.*?)\|(.*?)\]\]/i''<a href="http://www.mysite.com/$1" target="_blank">$2</a>'$string);
    return 
    $string;
    }


    print 
    url_links($text);

    ?>
    mlseim, it works great ... although I forgot to mention I also have a few in the string such as [[hyperlink]] that don't have the '|' in them in addition to the others and need them to be like:

    <a href="http://www.mysite.com/hyperlink" target="_blank">hyperlink</a>

    I tried adding the below code, although it didn't seem to work and the output gets jumbled up.

    PHP Code:
    <?php

    $text
    ="string is surrounded by other text and also contains the brackets exactly as shown ... [[please_parse|this part]] string is [[surrounded]] by other text and also contains the brackets exactly as shown ... [[also_parse|this second part]] string is surrounded by other text and also [[contains]] the brackets exactly as shown ... [[another_part|to parse]] 
    "
    ;

    function 
    url_links($string)
    {
    $string preg_replace('/\[\[(.*?)\|(.*?)\]\]/i''<a href="http://www.mysite.com/$1" target="_blank">$2</a>'$string);
    $string preg_replace('/\[\[(.*?)\]\]/i''<a href="http://www.mysite.com/$1" target="_blank">$1</a>'$string); // Added
    return $string;
    }

    print 
    url_links($text);

    ?>

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ... got it figured it out, although I'm not sure if it's the most effective way ...



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •