array string replacement code

Discussion in 'PHP' started by Luke Jones, Jul 23, 2007.

  1. #1
    Hello great computer chiefs!
    I'm using this code to remove links from an output page:

    function _replacement($text) {
    $array = array("A HREF" => " ", "a href" => " ", "<form>" => " ", "</form>" => " ");
    foreach($array as $original => $replacement) {
    $text = str_replace($original, $replacement, $text);
    }
    return $text;
    }

    I would like develop this code to remove all html and Javascript, so that only text is output (the text of an atricle).
    Any suggestions on how to do this?

    Thanks

    Luke.
     
    Luke Jones, Jul 23, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    First off, str_replace can take arrays as arguments, so you could do:

    
    function _replacement($text)
    {
    	$array = array("A HREF" => " ", "a href" => " ", "<form>" => " ", "</form>" => " ");
    
    	return str_replace(array_keys($array), array_values($array), $text);
    }
    
    PHP:
    And besides that, have a look at www.php.net/strip_tags
     
    nico_swd, Jul 23, 2007 IP
  3. Luke Jones

    Luke Jones Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello Nico,
    Yes, strip_tags would be the best thing, but as I mentioned on this thread, I can't get both the strip_tags and str_replace to work together. It's either one or the other.
    I'm not very experienced with php.
     
    Luke Jones, Jul 23, 2007 IP
  4. Luke Jones

    Luke Jones Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello!
    Please ignore the message above, I've just got it working with both functions!

    :)
     
    Luke Jones, Jul 23, 2007 IP
  5. KiruSoft

    KiruSoft Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    htmlentities() ?
     
    KiruSoft, Jul 23, 2007 IP
  6. Luke Jones

    Luke Jones Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what are htmlentities?
    I've never heard of them! :D
     
    Luke Jones, Jul 23, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Before you ask again what a function is for, or what it does, you have to bookmark following page:

    www.php.net

    From now on, this page is your new best friend. Type "htmlentities" in the search field and hit enter. The answer to your question lays there.

    And do the same in the future, when you come across functions you don't know.
     
    nico_swd, Jul 24, 2007 IP