I'm working with WinForms and I'm using RichTextBox to be able to use bold and italic.
When I insert into the DB I import it as HTML to be able to export them back as HTML later and to get the same font.
While trying to print a report I used the HTMLTextBox, but it seems that the control only admits simple markup language and will crash if used the HTML I'm getting.
So in order to complete my work, I set a non-adjustable fonts and only allowed him to use normal font, bold, or italic, that way the CSS Class will remain the same whenever he used the desired fonts.
I tried to set up a function that simplifies the HTML to be able to translate the HTML in order to be admitted in the HTMLTextBox.
I used this function but it's not working and I've been working on it for 2 days and I'm new to SQL.
DECLARE #HTMLpure nvarchar(max) ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled</title><style type="text/css"> .p_CC664AAA { margin: 0px 0px 12px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } .s_E5E150E4 { font-family: ''Calibri'';font-weight: bold;font-size: 16px;color: #000000; } .s_6BF1D20F { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000; } .s_F152B3A2 { font-family: ''Calibri'';font-style: italic;font-size: 16px;color: #000000; } .s_40C97128 { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000;text-decoration: underline; } </style></head><body><p class="p_CC664AAA"><span class="s_E5E150E4">BOLD</span><span class="s_6BF1D20F"> </span><span class="s_F152B3A2">iTALIC </span><span class="s_40C97128">UNDERLINE </span><span class="s_6BF1D20F">NORMAL</span></p></body></html>'
DECLARE #FinalHTML nvarchar(max)= ''
DECLARE #newHTMLpure nvarchar(max)
SET #HTMLpure = REPLACE (#HTMLpure, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled</title><style type="text/css">' , '')
set #HTMLpure = replace (#HTMLpure, '.p_CC664AAA { margin: 0px 0px 12px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } ','')
set #HTMLpure = replace (#HTMLpure, '.s_E5E150E4 { font-family: ''Calibri'';font-weight: bold;font-size: 16px;color: #000000; }','')
set #HTMLpure = replace (#HTMLpure , '.s_40C97128 { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000;text-decoration: underline; } ','')
set #HTMLpure = replace (#HTMLpure , '.s_6BF1D20F { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000; } ','')
set #HTMLpure = replace (#HTMLpure , '.s_F152B3A2 { font-family: ''Calibri'';font-style: italic;font-size: 16px;color: #000000; } ','')
set #HTMLpure = REPLACE (#HTMLpure , '.p_384D933F { margin: 0px 0px 0px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; }' , '')
set #HTMLpure = replace (#HTMLpure ,'</style></head><body><p class="p_CC664AAA">','')
set #HTMLpure = REPLACE (#HTMLpure ,'</style></head><body><p class="p_384D933F">' , '')
set #HTMLpure = replace (#HTMLpure ,'</p></body></html>','')
WHILE len(LTRIM(#HTMLpure)) > 0
BEGIN
IF LTRIM(#HTMLpure) like '<span class="s_E5E150E4">%'
BEGIN
SET #HTMLpure = replace (LTRIM(#HTMLpure) ,'<span class="s_E5E150E4">','<strong>')
SET #HTMLpure=Stuff(#HTMLpure, CharIndex('</span>', #HTMLpure), Len('</span>'), '</strong>')
SELECT #newHTMLpure = substring(LTRIM(#HTMLpure),charindex('<strong>',LTRIM(#HTMLpure))+len('<strong>'),charindex('</strong>',LTRIM(#HTMLpure))-charindex('<strong>',LTRIM(#HTMLpure))-len('<strong>'))
SET #HTMLpure = REPLACE(#HTMLpure,'<strong>' + #newHTMLpure + '</strong>','')
SET #newHTMLpure = '<strong>' + #newHTMLpure + '</strong>'
SET #FinalHTML = #newHTMLpure
SET #newHTMLpure = ''
END
ELSE IF LTRIM(#HTMLpure) like '<span class="s_F152B3A2">%'
BEGIN
SET #HTMLpure = replace (LTRIM(#HTMLpure) ,'<span class="s_F152B3A2">','<em>')
SET #HTMLpure=Stuff(LTRIM(#HTMLpure), CharIndex('</span>', LTRIM(#HTMLpure)), Len('</span>'), '</em>')
SELECT #newHTMLpure = substring(LTRIM(#HTMLpure),charindex('<em>',LTRIM(#HTMLpure))+len('<em>'),charindex('</em>',LTRIM(#HTMLpure))-charindex('<em>',LTRIM(#HTMLpure))-len('<em>'))
SET #HTMLpure = REPLACE(LTRIM(#HTMLpure),'<em>' + #newHTMLpure + '</em>','')
SET #newHTMLpure = '<em>' + #newHTMLpure + '</em>'
SET #FinalHTML = #FinalHTML + #newHTMLpure
SET #newHTMLpure = ''
END
ELSE IF LTRIM(#HTMLpure) like '<span class="s_40C97128">%'
BEGIN
SET #HTMLpure = replace (#HTMLpure ,'<span class="s_40C97128">','<u>')
SET #HTMLpure=Stuff(#HTMLpure, CharIndex('</span>', #HTMLpure), Len('</span>'), '</u>')
SELECT #newHTMLpure = substring(#HTMLpure,charindex('<u>',#HTMLpure)+len('<u>'),charindex('</u>',#HTMLpure)-charindex('<u>',#HTMLpure)-len('<u>'))
SET #HTMLpure = REPLACE(#HTMLpure,'<u>' + #newHTMLpure + '</u>','')
SET #newHTMLpure = '<u>' + #newHTMLpure + '</u>'
SET #FinalHTML = #FinalHTML + #newHTMLpure
SET #newHTMLpure = ''
END
ELSE IF LTRIM(#HTMLpure) like '<span class="s_6BF1D20F">%'
BEGIN
SET #HTMLpure=Stuff(LTRIM(#HTMLpure), CharIndex('<span class="s_6BF1D20F">', LTRIM(#HTMLpure)), Len('<span class="s_6BF1D20F">'), ' ')
SET #HTMLpure=Stuff(LTRIM(#HTMLpure), CharIndex('</span>', LTRIM(#HTMLpure)), Len('</span>'), ' ')
SET #HTMLpure = REPLACE(LTRIM(#HTMLpure),'' + #newHTMLpure + '','')
SELECT #newHTMLpure = substring(LTRIM(#HTMLpure),charindex(' ',LTRIM(#HTMLpure))+len(' '),charindex(' ',LTRIM(#HTMLpure))-charindex('% ',LTRIM(#HTMLpure))-len(' '))\
IF #newHTMLpure = ''
BEGIN
SET #newHTMLpure = '&NBSP;'
END
SET #FinalHTML = #FinalHTML + #newHTMLpure
SET #newHTMLpure = ''
END
SET #FinalHTML = #FinalHTML + ' ' + #newHTMLpure
END
SET #HTMLpure = #FinalHTML
END
I'm really sorry for this long code and I know I should have used vb coding instead of SQL but I'm taking it as a practice for me in SQL
Please help,
Thank you.
Okay a few things here.
Store the most abstract version of your data in your database. I.e. if you need to store information about the typesetting of text you need attributes such as face, size, is_bold, &c. This will allow your data to be applicable to all possible situations (you should be more likely to change your frontend than your data schema)
Logic for rendering data should be separate from that data. When you extract text with it's associated attributes (face...) the rendering into HTML, or any other presentation format such as whatever your TextBox expects, should be done as close to the point of presentation as possible.
Code related to presentation goes in your GUI.
Data goes in your database.
Related
I think it's quite simple to realize but i really dont know where to start. What i would like is an option in the form where people can add an image or other file.
Ive added this in my bootstrap form:
<!--attachments-->
<div class="form-group">
<label for="attachment">Attachment</label>
<input type="file" class="form-control-file" name="attachmentFile" id="attachmentFile">
</div>
So far so good i guess. But it wont send because there has to be something added in my phpmailer / send.php file. This is the script which i use now:
<?php
require 'PHPMailerAutoload.php';
// Controleren of het formulier is verzonden dmv submit
if (isset($_POST['submitted'])) {
$errors = array(); // Initialize error array.
// Geef hier zoveel mogelijk vereiste velden voor validatie
//Add attachment
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']
['tmp_name'],$_FILES['attachmentFile']['name']);
}
// Controleer naam
if (!preg_match("/^[a-zA-Z]+[a-zA-Z[:space:]]{2,}$/", $_POST['naam'])) {
$errors[] = 'Naam';
}
// Controleer adres
if (!preg_match("/^[a-zA-Z[:space:]]{1,}$/", $_POST['straat'])) {
$errors[] = 'Adres';
}
// Controleer huisnummer
if (!preg_match("/^[0-9]{1,4}$/", $_POST['huisnummer'])) {
$errors[] = 'Huisnummer';
}
// Controleer postcode
if (!preg_match("/^[0-9]{4}$/", $_POST['postcode'])) {
$errors[] = 'Postcode (Cijfers)';
}
// Controleer postcode letters
if (!preg_match("/^[a-zA-Z]{2}$/", $_POST['letters'])) {
$errors[] = 'Postcode (Letters)';
}
// Controleer woonplaats
if (!preg_match("/^[a-zA-Z]+[a-zA-Z[:space:]]{2,}$/", $_POST['woonplaats'])) {
$errors[] = 'Woonplaats';
}
// Controleer Kengetal
if (!preg_match("/^[0]{1}[0-9]{1,3}$/", $_POST['kengetal'])) {
$errors[] = 'Kengetal';
}
// Controleer telefoonnummer
if (!preg_match("/^[0-9]{6,8}$/", $_POST['telefoon'])) {
$errors[] = 'Telefoonnummer';
}
// Controleer e-mail adres
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*#([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,})$/", $_POST['email'])) {
$errors[] = 'E-mail adres';
}
// Controleer tekstvak
if (empty($_POST['bericht'])) {
$errors[] = 'Bericht';
}
if (empty($errors)) {
// Voer hier de gegevens in
$mailFrom = "myEmail";
$mailTo = "myEmail";
$senderNaam = $_POST['naam'];
$senderEmail = $_POST['email'];
$subject = 'mySubject';
$okMessage = '<p>blablabla</p>';
$errorMessage = '<p>blablabla</p>';
$emailText .= "Naam: " . $_POST['titel']." ". $_POST['naam']. "\n";
$emailText .= "Bedrijfsnaam: " . $_POST['bedrijfsnaam']. "\n";
$emailText .= "Adres: " . $_POST['straat']." ". $_POST['huisnummer']." ". $_POST['toevoeging']. "\n";
$emailText .= "Postcode: " . $_POST['postcode']." ". $_POST['letters']. "\n";
$emailText .= "Woonplaats: " . $_POST['woonplaats']. "\n";
$emailText .= "Telefoonnummer: " . $_POST['kengetal']." ". $_POST['telefoon']. "\n";
$emailText .= "E-mailadres: " . $_POST['email']. "\n";
$emailText .= "Bericht: " . $_POST['bericht']. "\n";
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'SMTP SERVER'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'User'; // SMTP username
$mail->Password = 'Pass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->Sender = $mailFrom;
$mail->SetFrom($senderEmail, $senderNaam);
$mail->AddReplyTo($senderEmail, $senderNaam);
$mail->Subject = $subject;
$mail->MsgHTML($emailText);
$mail->AddAddress($mailTo, $mailTo);
$mail->addAttachment($uploadfile, 'My uploaded file'); **// ADDED THIS HERE**
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = "<table>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Naam:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['titel']." ".$_POST['naam']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Bedrijfsnaam:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['bedrijfsnaam']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Adres:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['straat']." ".$_POST['huisnummer']." ".$_POST['toevoeging']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Postcode:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['postcode']." ".$_POST['letters']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Woonplaats:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['woonplaats']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Telefoonnummer:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['kengetal']." ".$_POST['telefoon']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>E-mailadres:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['email']."</td></tr>
<tr><td style='padding-left: 10px; padding-right: 10px; background: #eee;'>Bericht:</td><td style='padding-left: 10px; font-weight: bold;'>".$_POST['bericht']."</td></tr>
</table>";
$mail->AltBody = $emailText;
if(!$mail->send()) {
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
} else {
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
}
?>
Also added this to <form> enctype="multipart/form-data"
Edit #2: I also have a contact.js file with this:
data: $(this).serialize(),
I changed it in this:
data: new FormData(this),
Because i read somewhere that this could be the problem, but it didnt make sense.
Alright, the form is sending with attachments! There is only one little thing i hope someone can help me with that.. more about this later on. First the code.
In contact.js - I had to change data: $(this).serialize(), into data: new FormData(this),
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "send.php";
$.ajax({
type: "POST",
url: url,
data: new FormData(this),
cache : false,
contentType : false,
processData : false,
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset(); //Remove this line if form should not reset after submit
$('body, html').animate({scrollTop:$('#contact-form').offset().top}, 'fast'); //Remove this line if form should not scroll to top of the form after submit
}
}
});
return false;
}
})
});
Myform.html - 2 things: this enctype="multipart/form-data" in <form>, and this
<!--attachments-->
<div class="form-group">
<label for="attachment">Attachment</label>
<input type="file" class="form-control-file" name="attachmentFile" id="attachmentFile">
</div>
somewhere you want in the form.
Finally put this in your phpmailer file:
//Add attachment
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
I did it below $mail->AddAddress($mailTo, $mailTo);
So far how to fix this. There is still one problem left i refer to in the beginning.
Without data: new FormData(this), I still get my okMessage (with markup) when i hit send and the form stays at the same page.
With data: new FormData(this), it goes to a new page and just give black on white plain text (without markup).
So its like FormData is overruling something. Hope someone can help me out with this.
EDIT: Seems i have fixed this too. Place this..
cache : false,
contentType : false,
processData : false,
..below data: new FormData(this),
I have the following T-SQL job that I run - I need to pull invoices and credit notes into the one email - however, in two separate tables:
DECLARE #bodyMsg nvarchar(max)
DECLARE #subject nvarchar(max)
DECLARE #tableHTML nvarchar(max)
DECLARE #tableHTML2 nvarchar(max)
SET #subject = 'Invoices & Credits in Past Week'
SET #tableHTML =
N'<style type="text/css">
#box-table
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
text-align: center;
border-collapse: collapse;
border-top: 7px solid #9baff1;
border-bottom: 7px solid #9baff1;
border-spacing: 10px;
}
#box-table th
{
font-size: 13px;
font-weight: normal;
background: #b9c9fe;
border-right: 2px solid #9baff1;
border-left: 2px solid #9baff1;
border-bottom: 2px solid #9baff1;
color: #039;
border-spacing: 10px;
padding: 15px;
}
#box-table td
{
border-right: 1px solid #aabcfe;
border-left: 1px solid #aabcfe;
border-bottom: 1px solid #aabcfe;
color: #669;
border-spacing: 10px;
padding: 15px;
}
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
</style>'+
N'<H3><font color="Red">Invoices from Past week</H3>' +
N'<table id="box-table" >' +
N'<tr>
<th>Invoice #</th>
<th>Date</th>
<th>Account #</th>
<th>Customer</th>
<th>ex GST</th>
</tr>' +
CAST ( (
SELECT
td = OINV.DocNum ,'',
td =CONVERT(VARCHAR(8), OINV.DocDate, 3) ,'',
td = OINV.CardCode ,'',
td = OINV.CardName ,'',
td = '$' + CONVERT(varchar, CONVERT(money, OINV.DocTotal-OINV.VatSum-OINV.TotalExpns), 1)
FROM OINV INNER JOIN OSLP ON OINV.SlpCode = OSLP.SlpCode
WHERE OSLP.SlpName = 'SALES REP' and OINV.DocDate > DATEADD(DAY, -7, GETDATE())
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) )
#tableHTML2 =
N'<style type="text/css">
#box-table
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
text-align: center;
border-collapse: collapse;
border-top: 7px solid #9baff1;
border-bottom: 7px solid #9baff1;
border-spacing: 10px;
}
#box-table th
{
font-size: 13px;
font-weight: normal;
background: #b9c9fe;
border-right: 2px solid #9baff1;
border-left: 2px solid #9baff1;
border-bottom: 2px solid #9baff1;
color: #039;
border-spacing: 10px;
padding: 15px;
}
#box-table td
{
border-right: 1px solid #aabcfe;
border-left: 1px solid #aabcfe;
border-bottom: 1px solid #aabcfe;
color: #669;
border-spacing: 10px;
padding: 15px;
}
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
</style>'+
N'<H3><font color="Red">Credits from Past week</H3>' +
N'<table id="box-table" >' +
N'<tr>
<th>Credit #</th>
<th>Date</th>
<th>Account #</th>
<th>Customer</th>
<th>ex GST</th>
</tr>' +
CAST ( (
SELECT
td = ORIN.DocNum ,'',
td =CONVERT(VARCHAR(8), ORIN.DocDate, 3) ,'',
td = ORIN.CardCode ,'',
td = ORIN.CardName ,'',
td = '$' + CONVERT(varchar, CONVERT(money, ORIN.DocTotal-ORIN.VatSum-ORIN.TotalExpns), 1)
FROM ORIN INNER JOIN OSLP ON ORIN.SlpCode = OSLP.SlpCode
WHERE OSLP.SlpName = 'SALES REP' and ORIN.DocDate > DATEADD(DAY, -7, GETDATE())
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>'
EXEC msdb.dbo.sp_send_dbmail #recipients='email#email.com',
#profile_name = 'SQL Alerts',
#subject = #subject,
#body = #tableHTML,
#body = #tableHTML2,
#body_format = 'HTML' ;
This T-SQL script runs when I only have #tableHTML in the query.
I need to find out why the above is erroring?
Any help appreciated!
Regards
Rick
I am trying to extract a URL from a text file. I am using PowerShell to do this. The last part of the URL will be different each time. A snippet of the file is as follows:
<table class="button" style="border-collapse: collapse; border-spacing: 0; overflow:
hidden; padding: 0; text-align: left; vertical-align: top; width: 100%;"><tbody>
<tr style="padding: 0; text-align: left; vertical-align: top;"><td style="-moz-hyphens: none;
-webkit-hyphens: none; -webkit-text-size-adjust: none; background: #049FD9;
border: none; border-collapse: collapse !important; border-radius: 2px; color: #fff; display: block; font-family: 'Helvetica-Light','Arial',sans-serif; font-size: 14px; font-weight: lighter; hyphens: none; line-height:19px; margin: 0; padding: 8px 16px; text-align: center; vertical-align: top; width: auto
!important; word-break: keep-all;">
<a href="https://www.website.com:443/idb/setPassword?t=BcHJEoIgAADQD%2BKQjqZ4VEKtBHLJJm82uWDuxCR%2Bfe%2B58Rl9HRz6QddWkO5MLDXuF6e9m%2Bo0z%2FCVS%2B9IenAp5m5yTfYRa%2BAn4jdWHHF7HTyqRZiRRiNDEE%2BK7ZJywLKeNCTj4ewu4QNu02qXB0ZTXTyxXADwaLeluZGVPCxGXunpVcHbiCVAWRR7ykqGensLVBsqNUpl%2FQE%3D"
style="-webkit-text-size-adjust: none; font-weight: 100; color: #fff; font-family: 'Helvetica-Light','Arial',sans-serif; font-size: 20px; font-weight: lighter; line-height: 32px; text-decoration: none;">Get Started</a> </td></tr></tbody></table></td>
I want to extract the URL that starts with:
https://www.website.com:443/idb/setPassword
The string after the t= will be different each time. How can I extract the entire URL into a variable that I can then parse to get the info I need, which is the string of characters after the ?t=?
Try the following:
$content = Get-Content -Path 'C:\test.txt'
[regex]$regex = '(?<=href="https:\/\/www\.website\.com:443\/idb\/setPassword\?t=)(.*)(?=" )'
$regex.Matches($content).Value
In $content replace the path with your text file that contains the URL and update the $regex with the correct URL to the site.
This method use Regex to match before (?<= ) the websites URL and after (?= ), and then selects the text in the middle.
Here is a solution that uses a combination of Select-String with a regular expression to get the URL and the [system.uri] class to interrogate it.
$Text = get-content 'html-sample.txt'
$URLString = ((Select-String '(http[s]?)(:\/\/)([^\s,]+)(?=")' -Input $Text).Matches.Value)
#At this point $URL is a string with just the URL and querystring as requested
$URLString
#Heres how you might interrogate it
[system.uri]$URL = $URLString
$Token = ($URL.Query -split '=')[1]
$URL.host
$Token
Explanation:
Uses the regular expression (http[s]?)(:\/\/)([^\s,]+)(?=") with Select-String to extract the URL. Note this will only get the first match by default, use the -AllMatches switch of Select-String if you need to match multiple URLs and then you'll need to deal with each result via a ForEach loop.
Uses [system.uri] to cast the URL as a URI object.
Access the host property of the object to return the base URL.
Accesses the query property of the object to return the query string and replaces the '?t=' part of the string using a regex that only does the replace where it appears in the beginning of the string (^ token) and using backslashes to escape the other regex special characters.
here's another way by casting [xml] to read the file as an xmldocument....
$thisxml = [xml](gc .\hypertext.html)
then drill down to the node you want using xpath
$thisxpath = ($thisxml).SelectNodes("//table//tr//td//a").href
then cast [system.uri] to parse and select the uri pieces you want.
$thisuri = [System.Uri]$thisxpath | %{($_.Scheme + "://" + $_.host + $_.LocalPath)}
I'm having troubles accessing the font-weight property with DOM.
html =
(Ltrim
<html>
<head>
<style type="text/css">
#bar{
border: 1px solid black;
}
div {
font-weight: bold;
}
</style>
</head>
<body><div>foo</div><p id="bar">bar</p></body>
</html>
)
doc := ComObjCreate("HTMLfile")
doc.write(html)
msgbox % doc.styleSheets[0].rules[0].style.border ; "black 1px solid"
msgbox % doc.styleSheets[0].rules[1].style.font-weight ; nothing
msgbox % doc.styleSheets[0].rules[1].style["font-weight"] ; "[object]"
Is this supposed to be this way? Or should the last two message boxes show the value "bold"?
The - Minus sign is used in mathematical operations and cannot be used in a variable name, hence the error you are experiencing.
Try something like Object.style.fontWeight="value" as suggested at http://www.w3schools.com/jsref/prop_style_fontweight.asp
In my application I am reading an HTML page to a string. HTML Page contains many tags, new lines & many other characters.
<style type="text/css">
h3{
font-weight:bold;
line-height:18px;
font-family:Arial;
font-size:12px;
text-align:justify;
color:black;
background-color:transparent;
width:280px;
margin:0;
}
body{
font-weight:normal;
line-height:18px;
font-family:Arial;
font-size:12px;
text-align:justify;
color:black;
background-color:transparent;
width:280px;
}
</style>
I want to replace this string like following.
When we NSLog above HTML page, it will print as above looks. But What I want to NSLog is as follows
\t\t<style type="text/css">\n\t\t\th3{\n
font-weight:bold;
line-height:18px;
font-family:Arial;
font-size:12px;
text-align:justify;
color:black;
background-color:transparent;
width:280px;
margin:0;
}
body{
font-weight:normal;
line-height:18px;
font-family:Arial;
font-size:12px;
text-align:justify;
color:black;
background-color:transparent;
width:280px;
}
</style>
I mean including backslash characters. Is it possible? The reason behind this - I want to replace above style dynamically - but for replacing above I must have source to replace - How to get source including \n&\r characters ?
Look at the NSString reference. You'll need something like:
string = [oldString stringByReplacingOccurrencesofString: #"\n" withString: #"\\n"];
// repeat for \t and \r