SitePoint Sponsor

User Tag List

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

    space in contents only

    Code:
    code1
    
    function space($str) {
    $strOut = '';
    for ($i = 0; $i < strlen($str); $i++) {
    $strOut .= ( substr($str, $i, 1) == ' ') ? '&nbsp;' : substr($str, $i, 1);
    }
    return $strOut;
    }
    $function_space=0;
    
    $myVar='1 2  3';
    echo space($myVar);
    
    result1
    1&nbsp;2&nbsp;3
    The code1 above produces the result1 above(Please notice there are 2 spaces between 2 and 3).


    if the value of $myVar is like the below
    Code:
    <div style="color:red">1 2  3</div>
    The result of it will be '<div&nbsp;style="color:red">1&nbsp;2&nbsp;&nbsp;3</div>.

    I like to remove &nbsp; which is between open tag(<) and close tag(>).

    I can remove with the code "str_replace('div&nbsp;','div ',$myVar);" in the case of the above, but there are variety of selectors and properties inside tags.



    How can I remove all spaces &nbsp; which is between open tag(<) and close tag(>) ?
    I like to put &nbsp; into contents (
    Code:
    1 2  3
    ) only which is outside tags instead of inside tags.
    Last edited by dotJoon; Jun 27, 2012 at 02:31. Reason: display problem

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Well, you could do it in two steps....
    1. Replace all ' ' with &nbsp;
    2. Replace all &nbsp between < and > with ' '

    (Hint: preg_replace and preg_replace_callback)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Well, you could do it in two steps....
    1. Replace all ' ' with &nbsp;
    2. Replace all &nbsp; between < and > with ' '

    (Hint: preg_replace and preg_replace_callback)
    I can do Replace all ' ' with &nbsp; using the code below.
    Code:
    $myVar=str_replace(' ','&nbsp;',$myVar);
    Do you mean Replace all ' ' &nbsp; using preg_replace?
    And Replace all &nbsp between < and > with ' ' using preg_replace_callback?


    Which part of the page http://kr2.php.net/manual/en/functio...e-callback.php can Replace all &nbsp; between < and > with ' '?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    You can do the first step with str_replace, yes.
    You'll need str_replace in the callback of preg_replace_callback for the second step.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •