How to exclude   in regex?

Discussion in 'PHP' started by wiseboke, Aug 1, 2011.

  1. #1
    what I want is something like this
    /&nbsp;([^&nbsp;]*?)<\/TD>/
    but [^&nbsp;] not working,is there any way to slove this?thanks
     
    wiseboke, Aug 1, 2011 IP
  2. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Use \s for a single space
     
    kajol, Aug 1, 2011 IP
  3. wiseboke

    wiseboke Peon

    Messages:
    133
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi,thanks for your reply
    I tried \s before, not working
    here is the source code I want to parse
    I want to get the country name,you can see there is &nbsp; before country name,that's good,but the country name United States has a space,it's different from &nbsp;,I think,so maybe I can get the country name without problem if I can find a way to exlude &nbsp;
     
    wiseboke, Aug 1, 2011 IP
  4. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #4
    Why don't you use strrchr? It should be faster than your regex. It catches the last occurance of the "needle". You can use str_(i)replace to remove the leading '&nbsp;' and the traling '</td>'.

    
    $str = '<td class="Bold" align="CENTER">Decatur, GA&nbsp;&nbsp;30030&nbsp;&nbsp;United States</td>';
    echo str_ireplace(array('&nbsp;', '</td>',), '', (strrchr($str, '&nbsp;&nbsp;')));
    PHP:
     
    bogi, Aug 1, 2011 IP
  5. wiseboke

    wiseboke Peon

    Messages:
    133
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks,bogi,I finally figure it out myself,actually exclude ; in the string &nbsp; is enough,before I thought I need to exclude &nbsp;
     
    wiseboke, Aug 1, 2011 IP