Results 1 to 2 of 2

Thread: Using two functions on one echo?

  1. #1
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using two functions on one echo?

    I have two functions, one turns un-href links into working href links.


    PHP Code:
    <?php
    $reg_exUrl 
    "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    ?>

    The other replaces an array of words into a different word.


    PHP Code:
    <?php
    $replace 
    = array(
    'Cat' => 'Dog',
    );
    ?>

    This is how you echo both separately, the problem I'm having and need help with is combining both functions into one echo so both fixes apply to a single variable ($content).


    PHP Code:
    <?php echo str_replace_assoc($replace,$content); ?>

    <?php echo preg_replace(a$reg_exUrl'<a href="'.$url[0].'" target="_blank" class="outboundlink" rel="nofollow">'.$url[0].'</a>'$content); ?>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    first off, $reg_exUrl and $replace are not functions; they are variables (that contain strings). The functions in question are str_replace_assoc() (which is a user-defined function; from the looks of it I'm not sure why you don't simply use str_replace()) and preg_replace().

    To apply both functions, simply save the output in a variable instead of using echo:
    PHP Code:
    $content_1 str_replace_assoc($replace$content);
    $content_2 preg_replace($reg_exUrl'<a href="'.$url[0].'" target="_blank" class="outboundlink" rel="nofollow">'.$url[0].'</a>'$content_1);
    // although I don't know what $url[0] is or where it's coming from

    echo $content_2
    you should avoid outputting anything (e.g., using echo or print) as long as possible, so you don't run into problems like this. complete all of your php processing first, and only echo it out at the end.
    Last edited by traq; 09-04-2011 at 12:12 AM.

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
  •