Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
法一:使用StringBuilder或StringBuffer
最簡單且高效的方法是使用StringBuilder或StringBuffer類來反轉(zhuǎn)字符串。這兩個(gè)類提供了reverse()方法,可以方便地實(shí)現(xiàn)字符串的反轉(zhuǎn)。
String str = "Hello, World!";
StringBuilder sb = new StringBuilder(str);
String reversedStr = sb.reverse().toString();
System.out.println(reversedStr);
通過調(diào)用StringBuilder的reverse()方法,我們可以得到一個(gè)反轉(zhuǎn)后的字符串。注意,由于reverse()方法返回的是StringBuilder對象,需要通過toString()方法將其轉(zhuǎn)換成字符串。
方法二:使用遞歸
遞歸是一種有效的反轉(zhuǎn)字符串的方法。我們可以定義一個(gè)遞歸函數(shù),每次取字符串的最后一個(gè)字符,并將其與剩余部分連接起來。
public static String reverseString(String str) {
if (str.isEmpty())
return str;
return reverseString(str.substring(1)) + str.charAt(0);
}
String str = "Hello, World!";
String reversedStr = reverseString(str);
System.out.println(reversedStr);
通過不斷調(diào)用自身并取子字符串,我們可以逐步反轉(zhuǎn)整個(gè)字符串。這種方法雖然簡單,但當(dāng)處理大量字符串時(shí)可能會(huì)導(dǎo)致堆棧溢出的問題,因此需謹(jǐn)慎使用。
方法三:使用字符數(shù)組
另一種常見的反轉(zhuǎn)字符串的方法是將字符串轉(zhuǎn)換為字符數(shù)組,然后利用循環(huán)或交換元素的方式實(shí)現(xiàn)反轉(zhuǎn)。
public static String reverseString(String str) {
char[] charArray = str.toCharArray();
int start = 0;
int end = str.length() - 1;
while (start < end) {
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
start++;
end--;
}
return new String(charArray);
}
String str = "Hello, World!";
String reversedStr = reverseString(str);
System.out.println(reversedStr);
將字符串轉(zhuǎn)換為字符數(shù)組后,我們可以使用兩個(gè)指針分別指向數(shù)組的首尾,并依次交換對應(yīng)位置的元素。通過循環(huán)遍歷,最終得到反轉(zhuǎn)后的字符串。
方法四:使用Collections.reverse()
如果你希望使用Java提供的現(xiàn)成方法來反轉(zhuǎn)字符串,可以利用Collections類的reverse()方法。該方法可以反轉(zhuǎn)List類型的集合,因此我們可以先將字符串轉(zhuǎn)換為字符列表,然后調(diào)用reverse()方法進(jìn)行反轉(zhuǎn)。
String str = "Hello, World!";
List charList = new ArrayList<>();
for (char c : str.toCharArray()) {
charList.add(c);
}
Collections.reverse(charList);
StringBuilder sb = new StringBuilder();
for (Character c : charList) {
sb.append(c);
}
String reversedStr = sb.toString();
System.out.println(reversedStr);
通過將字符串轉(zhuǎn)換為字符列表,并利用Collections.reverse()方法對其進(jìn)行反轉(zhuǎn),我們可以得到一個(gè)反轉(zhuǎn)后的字符串。最后,使用StringBuilder來拼接字符列表中的元素,得到最終結(jié)果。
要使用的是wkhtmltopdf的Python封裝——pdfkit
安裝
1. Install python-pdfkit:
$ pip install pdfkit
2. Install wkhtmltopdf:
$ sudo apt-get install wkhtmltopdf
sudo yum intsall wkhtmltopdf
brew install Caskroom/cask/wkhtmltopdf
使用
一個(gè)簡單的例子:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
你也可以傳遞一個(gè)url或者文件名列表:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以傳遞一個(gè)打開的文件:
with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf')
如果你想對生成的PDF作進(jìn)一步處理, 你可以將其讀取到一個(gè)變量中:
# 設(shè)置輸出文件為False,將結(jié)果賦給一個(gè)變量
pdf = pdfkit.from_url('http://google.com', False)
你可以制定所有的 wkhtmltopdf 選項(xiàng) http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 你可以移除選項(xiàng)名字前面的 '--' .如果選項(xiàng)沒有值, 使用None, False or * 作為字典值:
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
默認(rèn)情況下, PDFKit 將會(huì)顯示所有的 wkhtmltopdf 輸出. 如果你不想看到這些信息,你需要傳遞一個(gè) quiet 選項(xiàng):
options = {
'quiet': ''
}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf的命令語法 , TOC 和 Cover 選項(xiàng)必須分開指定:
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
當(dāng)你轉(zhuǎn)換文件、或字符串的時(shí)候,你可以通過css選項(xiàng)指定擴(kuò)展的 CSS 文件。
# 單個(gè) CSS 文件
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
pdfkit.from_file('file.html', options=options, css=css)
你也可以通過你的HTML中的meta tags傳遞任意選項(xiàng):
body = """
<html>
<head>
<meta name="pdfkit-page-size" content="Legal"/>
<meta name="pdfkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape
配置
每個(gè)API調(diào)用都有一個(gè)可選的參數(shù)。這應(yīng)該是pdfkit.configuration()API 調(diào)用的一個(gè)實(shí)例. 采用configuration 選項(xiàng)作為初始化參數(shù)。可用的選項(xiàng)有:
示例 :針對wkhtmltopdf不在系統(tǒng)路徑中(不在$PATH里面)
PATH里面):
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)
問題
IOError:'No wkhtmltopdf executable found':
確保 wkhtmltopdf 在你的系統(tǒng)路徑中(PATH), 會(huì)通過 configuration進(jìn)行了配置 (詳情看上文描述)。 在Windows系統(tǒng)中使用where wkhtmltopdf命令 或 在 linux系統(tǒng)中使用 which wkhtmltopdf 會(huì)返回 wkhtmltopdf二進(jìn)制可執(zhí)行文件所在的確切位置.
如果出現(xiàn)這個(gè)錯(cuò)誤意味著 PDFKit不能處理一個(gè)輸入。你可以嘗試直接在錯(cuò)誤信息后面直接運(yùn)行一個(gè)命令來查看是什么導(dǎo)致了這個(gè)錯(cuò)誤 (某些版本的 wkhtmltopdf會(huì)因?yàn)槎五e(cuò)誤導(dǎo)致處理失敗)
確保兩項(xiàng):
1)、你的系統(tǒng)中有中文字體
2)、在html中加入
下面是我隨便寫的一個(gè)HTML表格:
<html>
<head><meta charset="UTF-8"></head>
<body>
<table width="400" border="1">
<tr>
<th align="left">Item....</th>
<th align="right">1</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">1.10</td>
</tr>
<tr>
<td align="left">化妝品</td>
<td align="right">.00</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">0.40</td>
</tr>
<tr>
<th align="left">tOTAL</th>
<th align="right">01.50</th>
</tr>
</table>
</body>
</html>
下面是生成的PDF截圖
HTML標(biāo)簽相關(guān)的字符串格式化
string nl2br ( string $string )
nl2br() 就是將\n 替換成 <br> //javascript對\n才能夠執(zhí)行換行,對</br>是不能執(zhí)行換行
htmlspecialchars() 把一些預(yù)定義的字符轉(zhuǎn)換為 HTML 實(shí)體。
string htmlspecialchars(string,quotestyle,[character-set])
轉(zhuǎn)換以下字符及對應(yīng)的實(shí)體
& (和號) 成為 &
" (雙引號) 成為 "
' (單引號) 成為 '
< (小于) 成為 <
> (大于) 成為 >
第二個(gè)參數(shù): ENT_COMPAT 只轉(zhuǎn)換雙引號, 保留單引號, 為默認(rèn)值 compat: 兼容性
ENT_QUOTES 同時(shí)轉(zhuǎn)換兩種引號 quotes: 引號
ENT_NOQUOTES 不對引號進(jìn)行轉(zhuǎn)換
<html>
<body>
<?php
$str = "John & \" 'Adams'";
echo htmlspecialchars($str, ENT_COMPAT);
echo "<br />";
echo htmlspecialchars($str, ENT_QUOTES);
echo "<br />";
echo htmlspecialchars($str, ENT_NOQUOTES);
?>
</body>
</html>
輸出結(jié)果:John & " 'Adams'
John & " 'Adams'
John & " 'Adams'
htmlentities() 可以將所有的非ASCII碼字符轉(zhuǎn)換為對應(yīng)的實(shí)體代碼;除字母、數(shù)字、\外, 漢字和鍵盤上其他字符都轉(zhuǎn)換
<?php
$str = "A 'quote' \" is <b>bold</b>" ;
echo htmlentities ( $str ); // 輸出后源代碼: A 'quote' is <b>bold</b>
echo htmlentities ( $str , ENT_QUOTES ); // 輸出后源代碼: A 'quote' is <b>bold</b>
?>
返回的結(jié)果:A 'quote' "is <b>bold</b>
A 'quote' "is <b>bold</b>
注意: htmlspecialchars()和htmlentities作用直接輸出HTML腳本
htmlspecialchars()和htmlentities()函數(shù)對于轉(zhuǎn)義字符"\"處理,不會(huì)轉(zhuǎn)義實(shí)體代碼,要么當(dāng)轉(zhuǎn)義字符對待,要么原樣輸出;
PHP中htmlentities和htmlspecialchars的區(qū)別
這兩個(gè)函數(shù)的功能都是轉(zhuǎn)換字符為HTML字符編碼, 特別是url和代碼字符串。防止字符標(biāo)記被瀏覽器執(zhí)行。
使用中文時(shí)沒什么區(qū)別, 但htmlentities會(huì)格式化中文字符使得中文輸入是亂碼。
htmlentities轉(zhuǎn)換所有的html標(biāo)記, htmlspecialchars只格式化& ' " < 和 > 這幾個(gè)特殊符號
addslashes() 在指定的預(yù)定義字符前添加反斜杠。
這些預(yù)定義字符是:單引號 (') 雙引號 (") 反斜杠 (\) NULL字符(\x00)
提示:該函數(shù)可用于為存儲在數(shù)據(jù)庫中的字符串以及數(shù)據(jù)庫查詢語句準(zhǔn)備合適的字符串。
注釋:默認(rèn)情況下,PHP 指令 magic_quotes_gpc 為 on,對所有的 GET、POST 和 COOKIE數(shù)據(jù)自動(dòng)運(yùn)行 addslashes()。
不要對已經(jīng)被magic_quotes_gpc轉(zhuǎn)義過的字符串使用 addslashes(),因?yàn)檫@樣會(huì)導(dǎo)致雙層轉(zhuǎn)義。
遇到這種情況時(shí)可以使用函數(shù) get_magic_quotes_gpc() 進(jìn)行檢測。(如:$c=(!get_magic_quotes_gpc())?addslashes($c):$c;)
在本例中,我們要向字符串中的預(yù)定義字符添加反斜杠:
<?php
$str = "Who's John Adams?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";
?>
輸出:
Who's John Adams? This is not safe in a database query.
Who\'s John Adams? This is safe in a database query.
<?php
header("Content-type:text/html; charset=utf-8");
$str = "wo are \x0a studying \x00 php";
echo $str;
echo "<br>";
echo addslashes($str);
?>
輸出:
wo are studying php
wo are studying >wo are studying \0 php< php
stripslashes() 刪除反斜線("\")
在提交的表單數(shù)據(jù)中 ' " \ 等字符前被自動(dòng)加上一個(gè)\ ,這是配置文件php.ini中選項(xiàng)magic_quotes_gpc在起作用,
默認(rèn)是打開的,如果不處理則將數(shù)據(jù)保存到數(shù)據(jù)庫時(shí),有可能會(huì)被數(shù)據(jù)庫誤當(dāng)成控制符號而引起錯(cuò)誤。
通常htmlspecialchars()和stripslashes()函數(shù)復(fù)合的方式,聯(lián)合處理表單中的提交的數(shù)據(jù)htmlspecialchars(stripslashes())
strip_tags()
string strip_tags ( string $str [, string $allowable_tags ] )
剝?nèi)?HTML、XML 以及 PHP 的標(biāo)簽。
<?php
echo strip_tags("Hello <b><i>world!</i></b>","<b>");
?>
輸出結(jié)果:Hello world!
實(shí)例:
<?php
$str = "<b>webserver;</b> & \ 'Linux' & Apache";
echo "$str"; //直接輸出
echo "<br/>";
echo htmlspecialchars($str,ENT_COMPAT); //只轉(zhuǎn)換雙引號,為默認(rèn)參數(shù)
echo "<br />";
echo htmlspecialchars($str,ENT_NOQUOTES); //不對引號進(jìn)行轉(zhuǎn)換
echo "<br />";
echo htmlspecialchars($str,ENT_QUOTES); //同時(shí)轉(zhuǎn)換單引號和雙引號
echo "<br />";
echo htmlentities($str); //將所有的非ASCII碼字符轉(zhuǎn)換為對應(yīng)的實(shí)體代碼
echo "<br />";
echo addslashes($str); //將" ' \ 字符前添加反斜線
echo "<br />";
echo stripslashes($str); //刪除反斜線
echo "<br />";
echo strip_tags($str); //刪除<html>標(biāo)記
?>
輸出結(jié)果:
webserver; & \ 'Linux' & Apache
*請認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。