SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    remove <br /> and &nbsp; between table tag

    Code:
    <?php
    $myText="This&nbsp;text&nbsp;is&nbsp;before&nbsp;the&nbsp;table<br />
    <table><br />
    &nbsp;&nbsp;<tr><br />
    &nbsp;&nbsp;&nbsp;&nbsp;<td>This&nbsp;text&nbsp;is&nbsp;in&nbsp;the&nbsp;
    table</td><br />
    &nbsp;&nbsp;</tr><br />
    </table><br />
    This&nbsp;text&nbsp;is&nbsp;after&nbsp;the&nbsp;table.";
    ?>
    I have myText like the above.

    I like to change the value of $myText like the below.
    Code:
    This&nbsp;text&nbsp;is&nbsp;before&nbsp;the&nbsp;table
    <table><tr><td>This&nbsp;text&nbsp;is&nbsp;in&nbsp;the&nbsp;table</td></tr>
    </table><br />
    This text&nbsp;is&nbsp;after&nbsp;the&nbsp;table.";

  2. #2
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use strip_tags() function.
    for more info on this::
    http://in2.php.net/manual/en/function.strip-tags.php

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by venkat6134 View Post
    use strip_tags() function.
    for more info on this::
    http://in2.php.net/manual/en/function.strip-tags.php
    strip_tag make it remove all html tags.

    But I like to leave all tags.
    Instead of removing tags,
    I like to remove <br /> and &nbsp; between <table> and <tr>
    I like to remove <br /> and &nbsp; between <tr> and <td>
    I like to remove <br /> and &nbsp; between </td> and </tr>
    I like to remove <br /> and &nbsp; between </tr> and </table>

  4. #4
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use strip_tags() function like this:::
    $txt=strip_tags($txt,'<br/>&nbsp;');

    This will allow what you want to do...

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by venkat6134 View Post
    use strip_tags() function like this:::
    $txt=strip_tags($txt,'<br/>&nbsp;');

    This will allow what you want to do...
    I have the code below at http://dot.kr/x-test/strip_tag1.php .
    Code:
    <?php
    $txt="This text is before the table<br />
    <table><br />
    &nbsp;&nbsp;<tr><br />
    &nbsp;&nbsp;&nbsp;&nbsp;<td>This text is in the table</td><br />
    &nbsp;&nbsp;</tr><br />
    </table>
    This text is after the table.";
    
    $txt=strip_tags($txt,'<br/>&nbsp;');
    echo $txt;
    ?>
    The following is the result of the code above.
    Code:
    result
    
    This text is before the table
    
    &nbsp;&nbsp;
    
    &nbsp;&nbsp;&nbsp;&nbsp;This text is in the table
    
    &nbsp;&nbsp;
    
    This text is after the table.
    My target result below is, I think, quite different from the result above.
    Code:
    target result
    
    This&nbsp;text&nbsp;is&nbsp;before&nbsp;the&nbsp;table
    <table><tr><td>This&nbsp;text&nbsp;is&nbsp;in&nbsp;the&nbsp;table</td></tr>
    </table><br />
    This text&nbsp;is&nbsp;after&nbsp;the&nbsp;table.";

  6. #6
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    means, you can generate sub-strings by finding place holders on each and then apply to this function....

  7. #7
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by venkat6134 View Post
    means, you can generate sub-strings by finding place holders on each and then apply to this function....
    I am afraid that I don't understand what you mean.

    Could you tell me a little more detail, please?

    What is place holders?
    How can I make it?

  8. #8
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    find the positions of <table> and </table> in the original text..
    After that you can get two sub-strings from original text as::::
    (1).Get sub-string of original text from starting position to the <table> position and
    (2).Get sub-string of original text from </table> position to the end position.

    then on those two new strings, you can apply the strip_tags() function...

  9. #9
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by venkat6134 View Post
    find the positions of <table> and </table> in the original text..
    Code:
    strpos($txt,'<table>') 
    strpos($txt,'</table>')
    I found the position of <table> is 37 and the position of </table> is 167 with the code above.


    Quote Originally Posted by venkat6134 View Post
    After that you can get two sub-strings from original text as::::
    What two sub-strings should I get from original text?

    I think there are three sub-strings like the follow.
    Code:
    from the first to 37, 
    from 37 to 167 
    and from 168 to the last

  10. #10
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for that 3 sub-strings, you can make 3 strip_tag functions based on your requirement.
    Means 2nd string contains table data, for that you can write strip_tag() function what tags to allow on the body of table data...
    Similarly, you can write for other strings also...

  11. #11
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    When in doubt, read the manual. strip_tags
    Ian Anderson
    www.siteguru.co.uk

  12. #12
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,209
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    $txt="This text is before the table<br />
    <table><br />
    &nbsp;&nbsp;<tr><br />
    &nbsp;&nbsp;&nbsp;&nbsp;<td>This text is in the table</td><br />
    &nbsp;&nbsp;</tr><br />
    </table>
    
    This text is after the table.";
    
    $openTablePos=strpos($txt,'<table>');
    $closeTablePos=strpos($txt,'</table>');
    
    $preTable=substr($txt,0,$openTablePos);
    $midTable=substr($txt,$openTablePos,$closeTablePos-$openTablePos+8);
    $postTable=substr($txt,strLen($preTable)+strLen($midTable),
    strLen($txt)-strLen($preTable)-strLen($midTable) );
    
    $tableTxt=strip_tags($midTable);
    
    $tdTxt=str_replace("&nbsp;","",$tableTxt);
    
    
    $tableWITHtxt="<table><tr><td>".$tdTxt."</td></tr></table>";
    
    echo $preTable.$tableWITHtxt.$postTable;
    ?>
    The code above gets my target result.
    It works fine.

    Now I like to ask you a little more developed question.

    How can I make my target result below with myText below ?
    Code:
    $myText="This text is before the table<br />
    <table><br />
    &nbsp;&nbsp;<tr><br />
    &nbsp;&nbsp;&nbsp;&nbsp;<td>This <span style='color:red'>text</span> 
    is in the table</td><br />
    &nbsp;&nbsp;</tr><br />
    </table>
    
    This text is after the table.";
    
    target result
    
    This text is before the table<br />
    
    <table><tr><td>
    
    This <span style='color:red'>text</span> is in the table
    
    </td></tr></table>
    
    This text is after the table.
    Can't the function strip_tag() do this, can it?

  13. #13
    SitePoint Evangelist venkat6134's Avatar
    Join Date
    Jul 2010
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is also same.
    By using strip_tags() function, you can get the result.
    Store that content in a variable, and pass it to the strip_tags() function as a first parameter, and specify the tags what you want to be allowed as the second parameter.


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
  •