Jquery和PHP函数

Jquery和PHP函数,php,jquery,Php,Jquery,如何将此PHP函数包含在jquery.html中 PHP函数: function trim_text($string, $limit, $break="<", $pad=" ...") { $words = explode(' ', htmlentities(strip_tags($string))); $countr = count($words); if ($countr <= 8) { return $string; }else{ // return with no cha

如何将此PHP函数包含在jquery.html中

PHP函数:

function trim_text($string, $limit, $break="<", $pad=" ...")
{
$words = explode(' ', htmlentities(strip_tags($string)));
$countr = count($words);
if ($countr <= 8)
{
return $string;
}else{


// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {

  $string = substr($string, 0, $breakpoint) . $pad;
}
}
$last_space = strrpos(substr($string, 0, $limit), ' ');
$string = substr($string, 0, $last_space);
$string = strip_tags($string);  
return $string.$pad;
}
}

function trim\u text($string,$limit,$break=“很多php函数都是用javascript重写的。你可以找到它们

使用javascript实现这个函数会更好。

如果此消息来自服务器,您可以在JS中发送前对其进行修剪,将要修剪的文本发送到将进行修剪的脚本。服务器应发回修剪后的字符串,例如:

var theText = $("#aTextarea").val();
$.post('trimscript.php', {text: theText}, function(response) {
    $('.text').html('<div>' + response + '</div>');
});
var theText=$(“#aTextarea”).val();
$.post('trimscript.php',{text:theText},函数(响应){
$('.text').html(''+response+'');
});
在PHP中,获取GET或POST变量,对其进行修剪,然后将其发回:

<?php
if(isset($_POST['text']) && !empty($_POST['text'])) {
    $text = trim_text($_POST['text']);
    echo $text;
}
?>


我认为这描述了所需内容的流程。当然,还有许多其他方法可以实现这一点,以上只是一个简单的例子。

您可以通过ajax调用将消息作为post参数传递到页面,让php解析并返回给您。一年多后,这个答案非常有用。非常酷的网站
<?php
if(isset($_POST['text']) && !empty($_POST['text'])) {
    $text = trim_text($_POST['text']);
    echo $text;
}
?>