SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strip_tags Help Needed on Multidimensional Array

    I have an array as such:

    PHP Code:
    array(4) {
      [
    0]=>
      array(
    1) {
        [
    0]=>
        
    string(75"<table BORDER=5 CELLSPACING=1 CELLPADDING=2><tr><td bgcolor=ff0000><center>Test test</center>"
        
      
    }
      [
    1]=>
      array(
    1) {
        [
    0]=>
        
    string(93"<table BORDER=5 CELLSPACING=1 CELLPADDING=2><tr><td bgcolor=ff0000><center>Testing</center>"
        
      
    }
      [
    2]=>
      array(
    1) {
        [
    0]=>
        
    string(194"Hello</center></td></tr></table><table BORDER=5 CELLSPACING=1 CELLPADDING=2><tr><td bgcolor=ff0000><center>More test</center>"
        
      
    }
      [
    3]=>
      array(
    1) {
        [
    0]=>
        
    string(93"<table BORDER=5 CELLSPACING=1 CELLPADDING=2><tr><td bgcolor=ff0000><center>123</center>"
        
      
    }

    I'm trying to get strip_tags to work, but no dice.

    I've tried these:

    PHP Code:
        foreach ($matched as $key => $value)

        {
          
    $newlist[$key] = strip_tags($newlist);
        } 
    and this

    PHP Code:
    $newlist strip_tags($newlist); 
    What's the proper way to get this to work on a multidimensional array?

  2. #2
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't it be:

    PHP Code:
    foreach ($matched as $key => $value){ 
        
    $newlist[$key] = strip_tags($value); 

    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is multidimensional array so it will be rather
    PHP Code:
    foreach ($matched as $key => $value){ 
        
    $newlist[$key] = strip_tags($value[0]); 


  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I think I'd go for something a little bit more on the recursive side, just in case the array is deeper that originally planned.
    PHP Code:
    <?php
    function strip_tags_deep($value){
        return 
    is_array($value) ? array_map('strip_tags_deep'$value) : strip_tags($value);
    }

    $array = array(
        array(
            
    '<strong>1</strong>'
        
    ),
        array(
            
    '<strong>2</strong>'
        
    ),
        array(
            
    '<strong>3</strong>'
        
    ),
        array(
            
    '<strong>4</strong>'
        
    )
    );

    print_r(
        
    strip_tags_deep($array)
    );

    /*
        Array
        (
            [0] => Array
                (
                    [0] => 1
                )
            [1] => Array
                (
                    [0] => 2
                )
            [2] => Array
                (
                    [0] => 3
                )
            [3] => Array
                (
                    [0] => 4
                )
        )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    I think I'd go for something a little bit more on the recursive side, just in case the array is deeper that originally planned.
    Something else on the recursive side:

    PHP Code:
    $array = array(
        array(
    '<strong>1</strong>'),
        array(
    '<strong>2</strong>'),
        array(
    '<strong>3</strong>'),
        array(
    '<strong>4</strong>')
    );

    function 
    my_filter(&$value$key){
        
    $value strip_tags($value);
    }

    array_walk_recursive($array'my_filter');
    var_dump($array); 
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Zealot virginiamonkeys's Avatar
    Join Date
    Jun 2003
    Location
    Stafford, Virginia, USA
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh.... well... I was tired when I posted that last night after having worked on my script for the whole day. So I neglected to post a few other important tidbits of my script. But I tried Salathe's solution (after trying a few other ones above) and his worked as I needed it to.

    Thanks for the help.


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
  •