php獲取文字
A. php如何截取指定兩個字元串之間的文字
$str='<h1 style="margin:0px">USB PC Joystick Game Controller</h1>';
preg_match_all('/<h1[^>]*>(.*)<\/h1>/',$str,$match);
print_r($match[1][0]);
B. php匹配中文文字,並給字元串的中文文字加粗
剛才參數寫錯了,現在應該可以了
<?php
$str = "中文123美麗abc開心。?我們";
$len=strlen($str);
for ($i=0;$i<$len;$i++)
{
if(ord(substr($str,$i, 1))>127)
{
$temp=substr($str, $i,2);
//輸出粗體字,具體忘了,你可以查一下
echo "對";
$i++;
}
else
echo substr($str, $i,1);
}
?>
C. 請幫我寫一個php正則表達式,提取兩個空格中間的文字
php有現成的trim函數給你用啊。
trim函數是取消字元串兩邊的空格。
$str =" 武則天 "
$str = trim($str);//重新賦值後就可以去除兩邊的空格了
D. 怎麼用php正則獲得a標簽內的文字啊
php中正則匹配只要使用這兩個函數:
preg_match_all
preg_match
這里使用preg_match_all,代碼如下:
$subject='<ahref="xxx.php">abc測試</a>';//假設這是需要匹配的字元串
$pattern='/<ahref="[^"]*"[^>]*>(.*)</a>/';//這是匹配的正則表達式
preg_match_all($pattern,$subject,$matches);//開始匹配,該函數會把匹配結果放入$matches數組中
echo"<pre>";
print_r($matches);
/**
結果是:
Array
(
[0]=>Array
(
[0]=>abc測試
)
[1]=>Array
(
[0]=>abc測試
)
)
*/
E. php 怎麼實現讀取word文檔內容,顯示到html上面能給個案例最好了,謝謝!
如果在win可以用com組件讀取:
// 建立一個指向新COM組件的索引
$word = new COM("word.application") or die("Can't start Word!");
// 顯示目前正在使用的Word的版本號
//echo "Loading Word, v. {$word->Version}<br>";
// 把它的可見性設置為0(假),如果要使它在最前端打開,使用1(真)
// to open the application in the forefront, use 1 (true)
//$word->Visible = 0;
//打?一個文檔
$word->Documents->OPen("d:a.doc");
//讀取文檔內容
$test= $word->ActiveDocument->content->Text;
echo $test;
echo "<br>";
//將文檔中需要換的變數更換一下
$test=str_replace("<{變數}>","這是變數",$test);
echo $test;
$word->Documents->Add();
// 在新文檔中添加文字
$word->Selection->TypeText("$test");
//把文檔保存在目錄中
$word->Documents[1]->SaveAs("d:/myweb/comtest.doc");
// 關閉與COM組件之間的連接
$word->Quit();linux可用antiword插件去實現
F. 從後往前截取字元串 php函數
用strrchr這個PHP自帶的函數,就是從後面取字元串。然後用substr把逗號去掉,就可以了。
$str=substr(strrchr($string,','),1);