Results 1 to 8 of 8

Thread: Parsing a string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321

    Question Parsing a string

    How do I take text between two symbols, and do something with it, for example;

    Taking these forums as an example, you can do [ url]http://www.alistairbaillie.co.uk[ /url]. (Without the sapces) and it automatically makes it a URL.

    I know I could change the [ url] to <a href=" and the [ /url] to ">Click me</a>

    but this doesn't give the same effect.

    If anyone knows how I can achieve this, i would be gratefull.

  2. #2
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734

  3. #3
    Lively Member DJ P@CkMaN's Avatar
    Join Date
    Jan 2002
    Location
    Burpengary, Queensland, Australia
    Posts
    95
    You would however need a more advanced replace function such as ereg_replace() if you wanted [ url=www.blah.com]Text[ /url]
    There's something I've noticed in the last year or so...
    Australian's are good at EVERYTHING !!!

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Check out this thread. Same concept.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    how about this code:

    Code:
    <?php
    $searcharray = array("/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/esiU",
             		     "/(\[)(url)(])([^\"]*)(\[\/url\])/esiU");
    $replacearray = array("showurl('\\5', '\\7')",
    					  "showurl('\\4', '\\4')");
    $str = "hello.net<br>
    		   http://www.vbworld.net";
    echo $str."<br><br>";
    $str=preg_replace($searcharray, $replacearray, $str);
    echo $str;
    
    function showurl($url, $hyperlink) {
    return "<a href=\"$url\">$hyperlink</a>";
    }
    ?>
    That will handle [url] & [url=] without trouble.

    Hope that's what you want

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    that didn't post correctly..

    quote it to get the actual code..

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Scotland, UK
    Posts
    321
    Originally posted by da_silvy
    how about this code:

    Code:
    <?php
    $searcharray = array("/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/esiU",
             		     "/(\[)(url)(])([^\"]*)(\[\/url\])/esiU");
    $replacearray = array("showurl('\\5', '\\7')",
    					  "showurl('\\4', '\\4')");
    $str = "hello.net<br>
    		   http://www.vbworld.net";
    echo $str."<br><br>";
    $str=preg_replace($searcharray, $replacearray, $str);
    echo $str;
    
    function showurl($url, $hyperlink) {
    return "<a href=\"$url\">$hyperlink</a>";
    }
    ?>
    That will handle [url] & [url=] without trouble.

    Hope that's what you want

    Thanks, thats exactly what i wanted

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    glad i could help

Posting Permissions

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



Click Here to Expand Forum to Full Width