PHP+Ajax做簡單的文本抽象處理

來源:互聯網
上載者:User

我覺得PHP和Perl最偉大的地方就在於其強大的pattern matching能力,這使得很多的應用非常簡單地就能實現,比如說文本處理。最近嘗試了一下,做了個簡單的線上文本處理的應用。 具體功能是:使用者提交一個URL,後台php將文本分段,給出第一句話,若段落中有多句,則給出一個提示click to read more,然後非同步請求伺服器(ajax)發回其餘文本。

實現當中將文主要組織成為一個結構分明的資料,首先想到的就是XML,事實也證明是個好想法。下面就是代碼,備忘,也供他人蔘考。

 

①提交url的表單

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><title>online abstraction</title><br /></head></p><p><body><br /><form action="online_abstract.php" method="post"><br /><h3>online abstract service</h3><br />abstract your article: <input type="text" name="url" value="input your url here" size="60" /><br /><input type="submit" value="submit" /><br /></form><br /></body><br /></html><br />

 

②伺服器端php程式

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><title>online abstract</title><br /><mce:script type="text/javascript"><!--<br />function retrive(dataSource,id)<br />{<br />//alert("hi");<br />//alert(id);<br />var XMLHttpRequestObj=false;<br />if(window.XMLHttpRequest)<br />{<br />XMLHttpRequestObj=new XMLHttpRequest();<br />}<br />else if(window.ActiveXObject)<br />{<br />XMLHttpRequestObj=new ActiveXObject("Microsoft.XMLHTTP");<br />}</p><p>if(XMLHttpRequestObj)<br />{<br />XMLHttpRequestObj.open("GET",dataSource);<br />XMLHttpRequestObj.onreadystatechange = function()<br />{<br />if(XMLHttpRequestObj.readyState==4 && XMLHttpRequestObj.status==200)<br />{<br />var obj=document.getElementById(id);<br />obj.innerHTML=XMLHttpRequestObj.responseText;<br />}<br />}<br />XMLHttpRequestObj.send(null);<br />}</p><p>}<br />// --></mce:script><br /></head></p><p><body><br /><!--<input type="button" onclick="a()">--><br /><?php<br />$url=$_POST[url];<br />$fp=fopen($url,"r") or die("can not open file");<br />$file=chop(file_get_contents($url));<br />fclose($fp);<br />$para_array=preg_split("//n/n+/",$file);<br />$num=1;<br />$xml_string="<?xml version=/"1.0/" encoding=/"utf-8/"?>/n<article>/n";<br />foreach($para_array as $a)<br />{<br />$sentence_array=preg_split("//.|/?|!/",$a);<br />if(count($sentence_array)>1)<br />{<br />echo "<h3>para".$num."</h3>";<br />echo "<span id=/"".$num."/">".$sentence_array[0].".</span><a onmousedown=/"retrive('retrive.php?para=".$num."','".$num."')/"><font color=/"blue/"> detail</font></a><br />/n";</p><p>}<br />else<br />{</p><p>echo "<h3>para".$num."</h3>";<br />echo "<span id=/"".$num."/">".$a."</sapn><br />/n";<br />}<br />$num++;<br />$xml_string=$xml_string."<para>/n".$a."/n</para>/n";<br />}<br />$xml_string=$xml_string."</article>/n";<br />$fp=fopen("article.xml","w+") or die("can not open xml file to write");<br />if(flock($fp,LOCK_EX))<br />{<br />fwrite($fp,$xml_string);<br />flock($fp,LOCK_UN);<br />}<br />else<br />{<br />echo "can not lock xml file";<br />}<br />fclose($fp);<br />/*$fp=fopen("toanalyze.txt","w+") or die ("can not open file to write");<br />if(flock($fp,LOCK_EX))<br />{<br />fwrite($fp,$file);<br />}<br />else<br />{<br />echo "can not lock file";<br />}<br />fclose($fp);*/<br />?><br /></body><br /></html><br />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.