This question already has answers here:
String attribute values in multiple lines, HTML
(6 answers)
Adding a linebreak in some attribute string(like src or href ) in HTML / XML source
(1 answer)
Closed 3 years ago.
I have an HTML element that looks something like,
<element src="http://lsdjflksjdfkjewiojeriowjekwjekfljsdkfjisdjrsekjfijsiejisjojfsjlfejeileldjfsleisldkjfsiejljefijeljslefifjfsleif">
</element>
See how long that is?
I want to break up the src link into multiple lines.
In Python you would just use \ to break a long string into multiple lines. In JavaScript, you just use + "rest of string" to break it into multiple lines. What is the case for HTML? The browser wants to interpret my attempt as white space...
So when I did:
<element
src="http://lsdjflksjdfkjewiojeriowjekwjekfljsdkf
jisdjrsekjfijsiejisjojfsjlfejeileldjfsleisldkjfsiejljefi
jeljslefifjfsleif">
</element>
It inserted a bunch of %20 where the line breaks were. What else should I try?
On a similar post on Stack Overflow that addresses this issue, it suggests using JavaScript to split the lines up, which seems like overkill, and the answer that suggested splitting after '/'s in the URL seems not to work unless you cram the entire element on the left side of the page which would cause horrible formatting issues... Otherwise, it still put spaces in the link.
Google URL Shortener has been retired and has transitioned to Firebase Dynamic Links.
But in general, using a link shortener for external resources is a pretty terrible idea. There will inevitably be some overhead in using an external link shortener, you'll be at their mercy in the event of an outage or EOL, and there may be the possibility of redirect/poison attacks as well.
Related
This question already has answers here:
Why is it such a bad idea to parse XML with regex? [closed]
(3 answers)
Closed 4 years ago.
Just to get this out of the way from the start, I have read that question, and I totally get that generally it's a bad idea, and that there are other better ways.
However, for my very simple use case, which is to retrieve all the anchor tag links on a page, I would've thought the following regex would suffice, and be far simpler and faster than a full blown HTML parser:
href="(.+)"
Now, am I overlooking some obvious fatal flaw, or would this be a good enough option for my very simple use case?
Answer: No, it’s a bit more complicated than that.
Why? That will likely grab more than the anchor tag links because href is used for more than just anchor tags. See this page for some examples.
There may also be tags with an href attribute which are commented out in the HTML.
Also, it could be in a JavaScript string for elements that get dynamically added to a page such as
var newElement = 'My Page
There are quirks you’d need to account for such as the ones mentioned in the comments by LGSon and Daniel Gale, amongst others, but this is probably enough to answer your question - no, it’s not that simple.
This question already has answers here:
Line break in HTML with '\n'
(10 answers)
Closed 5 years ago.
I know I am revisiting an old problem but I hope there are new answers. I am using angular/Modal to display a paragraph from a .json file. I need to insert a newline for each paragraph when displayed in the browser. I have tried \n ,\r, This is a sample of an entry:
"termsAndServiceContent": "1. TERMS OF USE: \n These Terms of Use (Terms) govern your access or use, from within the United States and its territories and possessions ...........\r <br/> By accessing or using the Services, you confirm your agreement to be bound by these Terms........",
I expected to get a new line for the \n and \r but nothing happens. How can I get the browser to recognize the escape characters?
I guess the question is quite similar and I did mentioned that its an old issue. I really didn't see this
https://stackoverflow.com/questions/39325414/line-break-in-html-with-n
question before but I still don't believe the answers address my issue. I am not using plain HTML I am using Angular and my render code looks like this:
<a (click)="showTermAndServicesWindow($event)">{{LOCALIZATION.tos}}</a>
where .tos is my .json file and that's all the HTML I want in the view....also I don't want to do any HTML templating in the .ts modules.
Use the HTML code <br> for new lines, and make sure you use <div [innerHTML]="theHtmlString"></div> to insert it as HTML.
This question already has answers here:
How do I display PHP code in HTML?
(4 answers)
Closed 8 years ago.
I want to post a sample of some code I've written. I'm aware of the <code></code> tags, but the problem is that the PHP inside will actually get interpreted and executed when the page loads. What is the way around this?
You should be encoding your entities anyway, which will prevent PHP from parsing them.
Instead of <?php, use <php.
See also: What Are The Reserved Characters In (X)HTML?
If you want to display the source of an entire PHP script, you can use the show_source() function. For example,
show_source("sample.php");
If you want to display the code of the current page, you can do something like
show_source(__FILE__);
You can also use hightlight_file() in place of show_source().
If you were looking to display just a random line of code, you can use htmlspecialchars() or htmlentities(). For example,
htmlspecialchars(" <b> This will be a bold text </b> ");
A tip on the aside: Please look around, and google atleast once before asking. :)
This question already has answers here:
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 8 years ago.
I am trying to build a regular expression to extract the text inside the HTML tag as shown below. However I have limited skills in regular expressions, and I'm having trouble building the string.
How can I extract the text from this tag:
text
That is just a sample of the HTML source of the page. Basically, I need a regex string to match the "text" inside of the <a> tag. Can anyone assist me with this? Thank you. I hope my question wasn't phrased too horribly.
UPDATE: Just for clarification, report_drilldown is absolute, but I don't really care if it's present in the regex as absolute or not.
145817 is a random 6 digit number that is actually a database id. "text" is just simple plain text, so it shouldn't be invalid HTML. Also, most people are saying that it's best to not use regex in this situation, so what would be best to use? Thanks so much!
The answer is... DON'T!
Use a library, such as this one
([^<]*)
This won't really solve the problem, but it may just barely scrape by. In particular, it's very brittle, the slightest change to the markup and it won't match. If report_drilldown isn't meant to be absolute, replace it with [^']*, and/or capture both it and the number if you need.
If you need something that parses HTML, then it's a bit of a nightmare if you have to deal with tag soup. If you were using Python, I'd suggest BeautifulSoup, but I don't know something similar for C#. (Anyone know of a similar tag soup parsing library for C#?)
I agree regex might not be the best way to parse this, but using backreference it's easily done:
<(?<tag>\w*)(?:.*)>(?<text>.*)</\k<tag>>
Where tag and text are named capture groups.
hat-tip: expresso library
<a href\=\"[^\x00]*?\">
should get you the opening tag.
<\/a>
will give you the closing tag. Just extract out what is in between. Untested though.
In my web application I intend to shorten a lengthy string of HTML formatted text if it is more than 300 characters long and then display the 300 characters and a Read More link on the page.
The issue I came across is when the 300 character limit is reached inside an HTML tag, example: (look for HERE)
<a hreHERE="somewhere">link</a>
<a hre="somewhere">liHEREnk</a>
When this happens, the entire page could become ill-formatted because everything after the HERE in the previous example is removed and the HTML tag is kept open.
I thinking of using CSS to hide any overflow beyond a certain limit and create the "Read More" link if the text is beyond a certain number, but this would entail me including all the text on the page.
I've also thought about splitting the text at . to ensure that it's split at the end of a sentence, but that would mean I would include more characters than I needed.
Is there a better way to accomplish this?
Note: I have not specified a server side language because this is more of a general question, but I'm using ASP.NET/C# .
Extract the plaintext from the HTML, and display that. There are libraries (like the HTML Agility Pack for .NET) that make this easy, and it's not too hard to do it yourself with an XML parser. Trying to fix a truncated HTML snippet is a losing cause.
One option I can think of is to cut it off at 300 characters and make sure the last index of '<' is less than the last index of '>'. If it is, truncate the string right before the last instance of '>', then use a library like tidy html to fix tags that are orphaned (like the </a> in the example).
There are problems with this though. One thing being if there are 300 chars worth of nothing but HTML - your summary will be displayed as empty.
If you do not need the html to be displayed it's far easier to simply extract the plain text and use that instead.
EDIT: Added using something like tidy html for orphaned tags. Original answer only solved cutting thing mid-tag, rather than within an opening/closing tag.