SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: preg_replace()

  1. #1
    SitePoint Addict LiveTronix's Avatar
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace()

    This works.
    PHP Code:
    <?php
    $string 
    "{test2}{test} hey there {name}";
     
    function 
    insertVariables($string) {
    $string preg_replace("/({)(.*)(})/smU""<b>\\2</b>",$string);
    return 
    $string;
    }
     
    echo 
    insertVariables($string);
     
    ?>
    I want to do this:
    PHP Code:
    <?php
    $string 
    "{test2}{test} hey there {name}";
     
    function 
    insertVariables($string) {
    $string preg_replace("/({)(.*)(})/smU"pullVariable(\\2), $string);
    return 
    $string;
    }
     
    function 
    pullVariable($variable) {
    return 
    "<b>$variable</b>";
    // Do some other magical crap
    }
     
    ?>

  2. #2
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested this on the command line, it worked for me. All I did was put quotes around the slash-slash-two.

    PHP Code:
    <?php
    $string 
    "{test2}{test} hey there {name}";
     
    function 
    insertVariables($string) {
    $string preg_replace("/({)(.*)(})/smU"pullVariable("\\2"), $string);
    return 
    $string;
    }
     
    function 
    pullVariable($variable) {
    return 
    "<b>$variable</b>";
    // Do some other magical crap
    }
     
    ?>

  3. #3
    SitePoint Addict LiveTronix's Avatar
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Escape, you're right that works.

    I think this is a PHP bug though:
    PHP Code:
    <?php 
    $string 
    "{test2}{test} hey there {name}"
    function 
    insertVariables($string$ProjectID$_SAvars) { 
     
    $a 1;
     
    $b 1;
     
    $string preg_replace("/({)(.*)(})/smU"pullVariable("[url="file://\2"]\\2[/url]", $a, $b), $string); 
     
    return $string

    function 
    pullVariable($variable$a$b) { 
     echo 
    $variable."<br>";
     return 
    "<b>$variable</b>"
     
    // Do some other magical crap 

    echo 
    insertVariables($string2$_SAvars);
    ?>
    Check this out:
    http://www.saleassistant.com/survey/test.php

    Very Odd.

  4. #4
    SitePoint Zealot ZangBunny's Avatar
    Join Date
    Jul 2003
    Location
    Mainz, Germany
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not just use preg_replace_callback?
    Things that try to look like Things, sometimes
    look more like Things than Things. - Granny Weatherwax

  5. #5
    SitePoint Addict LiveTronix's Avatar
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Zang, that is why I posted here. I only know so much.

    It looks like that solution will work. I'll try it in the morning, thanks!

  6. #6
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, mighty weird stuff. :-)

    Best of luck to you.

  7. #7
    SitePoint Addict LiveTronix's Avatar
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I got it going.


Bookmarks

Posting Permissions

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