SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preg replace help

    HI,

    I am trying to echo out a html template page that is stored in the database but need to omit some data before it echoes, this is the method:

    PHP Code:
    $newbody $row2['body'];    
    $newtext1 "bbbbb";

    // use preg_replace to replace text
    $newbody preg_replace ('#%%NAME%%#'$newtext1$newbody);    

    echo 
    $newbody

    But I need it to find the text below not this '('#%%NAME%%#','
    Dear %%NAME%%,

    I tried doing this but get a blank page error when trying to load page:


    PHP Code:
    $newtext1 "bbbbb";
    $replacetext1 "Dear %%NAME%%,";
    $newbody preg_replace ($replacetext1$newtext1$newbody); 
    Can anyone please help?

  2. #2
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it sorted using str_replace

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think strtr() function becomes handy in such cases:
    PHP Code:
    <?php
    $trans 
    = array("hello" => "hi""hi" => "hello");
    echo 
    strtr("hi all, I said hello"$trans);
    //prints hello all, I said hi
    ?>


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
  •