How can I replace a newline in a html link?

Discussion in 'PHP' started by qwikad.com, Sep 7, 2012.

  1. #1
    For my text editor, I need to replace all new lines in any html link. I want all links to be formed correctly, that is. For instance this is a correct link:

    
    
    <a href="http://something.com"><img src="http://something.com/images/image.png" width="200" height="100"></a>
    
    
    Code (markup):

    But, let's say, if the same link looks something like this (notice how it has new lines):


    
    
    <a href="http://something.com"><img 
    src="http://something.com/images/image.png" width="200" 
    height="100"></a>
    
    
    Code (markup):
    I want those new lines to return to the original position. What preg_replace code can accomplish this?


    Thanks!
     
    qwikad.com, Sep 7, 2012 IP
  2. shubhamm

    shubhamm Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    use Notepad++ and use it function Find & Replace

    \r\n
    Code (markup):
    and about 2nd Prob. use a If statement if it Contain or line Equal to...
     
    shubhamm, Sep 8, 2012 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Since everything that should be in quotes without breaks between them IS, why are you kvetching over something so silly? It's not like the one you're calling wrong or 'formed incorrectly' is invalid markup and perfectly fine.

    Hell, if i was writing that same code, I would INTENTIONALLY format it thus:

    
    <a href="http://something.com">
    	<img
    		src="http://something.com/images/image.png"
    		width="200" height="100"
    		alt="Don't forget to describe the image!"
    	/>
    </a>
    Code (markup):
    Since it's easier to read than the idiotic cramming of everything onto one line... which is how people make mistakes like skipping required attributes or not realizing you've declared the same thing more than once. (This is far more of an issue in CSS)

    Using any automated tool to change the formatting as you ask? Just going to make a absolute mess of the code -- of course if it doesn't have any proper indentation or sensible formatting you're already in that particular boat.

    In any case, there's NOTHING 'malformed' or 'incorrect' about the latter of your code blocks.
     
    deathshadow, Sep 8, 2012 IP
    umeedngo likes this.
  4. hackedcpu

    hackedcpu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Since this is a PHP thread here's what might work...

    $stuff=str_replace(" \n"," ",$stuff);
    PHP:
    You will have to figure out if you have " \r" or " \r\n" in your file.
     
    hackedcpu, Sep 8, 2012 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    I'd assume he doesn't want to strip ALL linefeeds, just the ones inside elements like anchors, hence the asking about regex.
     
    deathshadow, Sep 8, 2012 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #6
    You are right, I want new lines in html links (between < and >) to be stripped. For instance I use this code to convert any quotation marks in links from symbols to real quotation marks:

    
    
                    $text = preg_replace('/<([^<>]+)>/e', '"<" .str_replace("&quot;", \'"\', "$1").">"', $text);
    
    
    Code (markup):

    Using the same concept, can you re-do this code and make it so that it would strip new lines in the html links? Thank you again.
     
    qwikad.com, Sep 9, 2012 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #7
    Hello...? Anyone can help me with this?
     
    qwikad.com, Sep 12, 2012 IP
  8. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    turn off word-wrap?
    What editor are you using?
     
    ezprint2008, Sep 13, 2012 IP
  9. umeedngo

    umeedngo Peon

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The code you are saying above have no problem in using HTML and it will also work..you know HTML requires no formating and no case -senstive.
     
    umeedngo, Sep 13, 2012 IP
  10. yashdev

    yashdev Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    hi,
    i knock that about php but i am not about your quesion
     
    yashdev, Sep 14, 2012 IP
  11. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #11
    Wow, you guys are making everything SOOOOO complicated. So far I have been only lectured on what I should or shouldn't do.

    Ok, here's my question to some AWESOME php pro: Is there a preg_replace code that you know of that will replace new lines ONLY in HTML codes??

    It should go like the one below but instead of replacing &quot; it should replace new lines. Thank you for your help!

    [COLOR=#111111]$text = preg_replace('/<([^<>]+)>/e', '"<" .str_replace("&quot;", \'"\', "$1").">"', $text);
    Code (markup):
    [/COLOR]

    Seriously, I need a solution to this thing, not lecturing.
     
    qwikad.com, Sep 22, 2012 IP