SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegExp help: add a space after ; unless part of specialchars

    I need to add a space after all instances of ";" symbol in a text except if it is part of one of the special characters converted by htmlspecialchars() and or of a non-breaking space.

    Can somebody suggest/help the correct syntax?

    I have tried this with no luck:

    $interview = preg_replace("/(^[&gt|&lt|&#039|&quot|&amp|&nbsp]+;)/si","\\1 ",$interview);

    Thanks for your suggestions

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi marcnyc

    I don't think you can do this in one line, more like three.

    Do you want to do this after the <,>,&,'," and non-breaking spaces have been encoded by htmlspecialchars, or before?

    If after then this should work:

    PHP Code:
    $interview preg_replace("(/&[^;]+;)/si","\\1^",$interview);
    $interview preg_replace("/(;)[^\^]/si","\\1 ",$interview);
    $interview preg_replace("/(;)\^"/si","\\1",$interview); 
    This replaces any ; that you want to keep with ;^, then adds spaces to all the other ; and then reverts the first set of ;^ back to ;, assuming that the original string doesn't have any of these.

    I haven't tested this code, but you should be able to use the idea to solve your problem.

    Paul
    Paul Davey
    webmaster for Whitford Church of Christ


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
  •