1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Reduce multiple tags to just one occurrence if there are no words between them with preg_replace

Discussion in 'PHP' started by qwikad.com, Oct 23, 2015.

  1. #1
    I searched the web up and down couldn't find anything that would be a working solution.

    Let's say I have multiple occurrence of
    <u></u><font color="green"></font><i></i><div></div><big></big>
    Code (markup):
    like this:

    <u></u><font color="green"></font><i></i><div></div><big></big><u></u><font color="green"></font><i></i><div></div><big></big><u></u><font color="green"></font><i></i><div></div><big></big><u></u><font color="green"></font><i></i><div></div><big></big><u></u><font color="green"></font><i></i><div></div><big></big>
    Code (markup):
    I want it to be reduced to just one occurrence:
    <u></u><font color="green"></font><i></i><div></div><big></big>
    Code (markup):
    The tags are not always the same. Basically I want multiple occurrences of tags that repeat again and again with no words between them to be reduced to just one occurrence. Hope it makes sense.
     
    qwikad.com, Oct 23, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I would think if they were empty you'd want to remove them entirely, since, well...

    NOT 100% sure that a regex is gonna get the job done without being so convoluted you'd not be better off brute forcing it. If they tags were NESTED or their closing's butted up against their openings, then I could see it... like reducing "<i><i>Book title</i></i>" or "<b>Something</b> <b>else</b>", but in your example reducing them to just one? Not sure the point as I'd be removing them entirely since, well... empty. Could you get away with just stripping out empty ones entirely since unless you're using them as something like a sandbag, there's no legitimate reason to leave tags without content in the markup... THAT would be far simpler to detect/remove using regex.

    NOT that <u> or <font> has any damned business on any website written after 1997. You going to have <center> in there as well?

    But I know you're working on a client side WYSIWYG, one of those things that IMHO if you "Need" on a site, the people working on the site have no damned business working on a site.
     
    deathshadow, Oct 23, 2015 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    There are no words in the them, just a multiple repetitious occurrence like:
    <u></u><font color="green"></font><i></i><div></div><big></big>
    <u></u><font color="green"></font><i></i><div></div><big></big>
    <u></u><font color="green"></font><i></i><div></div><big></big>
    <u></u><font color="green"></font><i></i><div></div><big></big>

    How would you reduce to just one
    <u></u><font color="green"></font><i></i><div></div><big></big> ?

    I know it should be easy I am just dumb.
     
    qwikad.com, Oct 23, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    If you know the exact sequence, just use str_replace... but again, why leave them in there AT ALL if there's nothing in them? Why are you reducing it to just one, what legitimate purpose could that serve?
     
    deathshadow, Oct 23, 2015 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #5
    And yes it is for a rich editor I installed. It works fine, it just has this bug that when you select a font / bold / italic etc. and then you do new lines it repetitiously prints the tags you selected. F*ck. I just want it to be only when there are words involved.
     
    qwikad.com, Oct 23, 2015 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #6
    The sequence is not always the same. The tags are not always the same. It should be something like strip all tags but leave one occurrence. Probably not possible.
     
    qwikad.com, Oct 23, 2015 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    That behavior is pretty typical of WYSIWYGS and, well... exists kind-of for a reason; inline-level containers can't wrap block level. Again, I'd just strip ALL the empty tags since they serve no purpose.

    It's also why I don't believe in putting WYSIWYG's on websites unless it's doing something like generating bbCode. Been there, done that, and it lets far too many people who have NO business work in HTML start sleazing out HTML any-old-way. At some point you have to put a "You have to be this smart to go on the ride" next to the door -- at the very least for their safety, ideally more for your own protection.
     
    deathshadow, Oct 23, 2015 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    That's the part I'm not getting, WHY do you want to leave ANY of them. They're empty, get rid of them! ALL of them.
     
    deathshadow, Oct 23, 2015 IP
  9. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #9
    Ok, let me ask you this then. How to strip all empty tags except <div></div><br> ?
     
    qwikad.com, Oct 23, 2015 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Why does it need <div></div> if it has a BR? Just leave the BR.

    $string = preg_replace(
      '/<(\w+)\b(?:\s+[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|"[^"]*"|[\w\-.:]+))?)*\s*\/?>\s*<\/\1\s*>/',
      '',
      $string
    );
    Code (markup):
    Should kill off all empty tags that have the closing model, while leaving standalone tags intact. No guarantees though, lifted that off stackOverflow.
    http://stackoverflow.com/questions/21051428/regex-to-remove-all-empty-html-tags

    If you REALLY need that DIV (for Christmas only knows what) it might be faster to pull all instances and then plug it in after detecting the <br>. (which the above regex SHOULD exclude)
     
    deathshadow, Oct 23, 2015 IP