SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reg exp help, not that easy ...

    well, while trying validating a page for Doctype strict, I found that I can't use & anywhere, and that should be replaced by &

    Problem is, I've in my page many ASCII characters like é   etc. And, then I have, say R&D or Taylor&Co.

    Is there a way that will translate the &s from R&D or Taylor&Co or Johnson&Sons but not those of é or  ? Knowing that someone may type Johnson &sons (with a space before & followed by a small letter) in lieu of Johnson&Sons.

    I guess preg_replace is the way to go here, but ...

  2. #2
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    This might be one way not fool proof
    Code php:
    $result = preg_replace('/([a-z])&([a-z])/i', '\\1&\\2', $subject);
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm ... thanks logic earth, that gives a good idea.

  4. #4
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Hmm I looked this over your best bet will probably be to encase all html entities in delimiters like: {! !} that will make it easier to single out &.

    Code php:
    $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
    $data = preg_replace('~(' . join('|', $trans) . ')~i', '{!\1!}', $data);

    then my first regex will work on stuff&text text
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, that seems to be a nice idea, never used get_html_translation_table earlier. Thanks again buddy.

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to add sum in regexp by the way, like

    PHP Code:
    $content preg_replace('/<font(.*?)size="(.*?)"(.*?)>(.*?)<\/font>/i''<span$1style="font-size:$2/3em"$3>$4</span>'$content); 
    Like, if someone enters font size 3, that will be translated as font size 1em (3 / 3). Any idea?

  7. #7
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That might require preg_replace_callback
    http://www.php.net/manual/en/functio...e-callback.php

    Afraid I don't have much experience with it.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried, it seems to be 'not so easy'


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
  •