Help with PHP Replace command

Discussion in 'PHP' started by phantomddl, Sep 16, 2007.

  1. #1
    i have a php file that shows recent searches on my site and displays, i am including it on my homepage with the code below:
    but sometimes people search bad words and i want to replace them with ***
    let's say someone searches "sex" and i want to show it as ***. is it possible to do it without editing recent.php? with str_replace command next to include.
    if so, how?
     
    phantomddl, Sep 16, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Probably. But why not just edit this file? It'd be way easier and a much better practice.
     
    nico_swd, Sep 16, 2007 IP
  3. phantomddl

    phantomddl Well-Known Member

    Messages:
    2,856
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    160
    Articles:
    15
    #3
    i hired a coder to do that recent.php file, but it was an encoded file and i didnt wanna pay $5 more for decoded one as i thought i wouldn't need to change anything. thats why ^^
     
    phantomddl, Sep 16, 2007 IP
  4. guruhowto

    guruhowto Guest

    Messages:
    75
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's possible but it depends...

    If the data is displayed inside recent.php then you'll have to edit it. But if recent.php only produces the final data to be displayed and the displaying of the data takes place outside recent.php then you can just look for that variable inside recent.php which is holding the final data to be displayed and include your str_replace just right below your include('recent.php');
     
    guruhowto, Sep 16, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Okay, I see.

    
    
    $badwords = array('sex', 'fuck', 'smurf');
    
    ob_start();
    include("recent.php");
    
    $content = preg_replace(
        '/(' . implode('|', array_map('preg_quote', $badwords)) . ')/ie',
        'str_repeat("*", strlen("$1"));',
        ob_get_contents()
    );
    ob_end_clean();
    
    
    echo $content;
    
    PHP:

    Untested but should work.
     
    nico_swd, Sep 16, 2007 IP
    phantomddl likes this.
  6. phantomddl

    phantomddl Well-Known Member

    Messages:
    2,856
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    160
    Articles:
    15
    #6
    thanks works!
     
    phantomddl, Sep 16, 2007 IP