2

我需要在每个之前添加一个空格

<a

如果还没有空格,则在带有 php 的字符串中。

所以这

hello world <a

应该保持原样

但是这个

hello world<a

应该和第一个例子一样。

感谢您的输入!


使用 ajax 重新加载页面以创建实时聊天

我有这个代码:

<?php require_once("config.inc.php"); ?>
<?
ob_start();
session_start();
if(isset($_SESSION['myusername'])) {
// do nothing here
} else { ?>
<!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>Live chat</title>
</head>
<body bgcolor="#000000">
<font color="white" size="+3"><b>You are not logged in! <br />
Log in and start chatting!</b></font>
</body>
</html>
<?php exit(); }
ob_flush();
?>







<!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" />
<meta name="description" content="<?php echo $meta_description ?>" />
<meta name="keywords" content="<?php echo $meta_keywords ?>" />
<title><?php echo $site_name; ?> | Live chat</title>
<link rel="icon" type="image/gif" href="favicon.png" >
<link href="style.css" rel="stylesheet" type="text/css" />
<style>
    body { margin:0;padding:0;  background-image:url(images/background.jpg);  }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
         $("#lolmesazhet").load("mesazhetnechat.php");
   var refreshId = setInterval(function() {
      $("#lolmesazhet").load("mesazhetnechat.php");
   }, 1000);
   $.ajaxSetup({ cache: false });
});
</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="500px" bgcolor="#000000" >
    <?php include 'headerchat.inc.php'; ?>
    </td>
  </tr>
<tr>
    <td width="500px"  align="left" valign="top" style="padding:20px 5px 20px;">
<?php
if($_POST['submit654']) {

$result128 = mysqli_query($con,"SELECT id FROM users WHERE email = '$_SESSION[myusername]'");
$row128 = mysqli_fetch_array($result128);

date_default_timezone_set("Europe/Tirane");
$todaydate3 = date("Y-m-d H:i:s");

mysqli_query($con,"INSERT INTO chat (id, derguesi, dhoma, mesazhi, ora) VALUES (NULL, '$row128[id]', 'Main room', '$_POST[mesazhi]', '$todaydate3')");
}
?>
<table width="470px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="50px" valign="middle" colspan="2" style="border-bottom:1px solid #FFFFFF;">&nbsp;</td>
  </tr>
  <tr>
    <td width="320" height="339px" valign="top" style="padding:3px;">
<div style="width:320px; height:339px; overflow-y:auto;">
<?php
$result73 = mysqli_query($con,"SELECT * FROM chat WHERE dhoma = 'Main room' ORDER BY id DESC");
while($row73 = mysqli_fetch_array($result73))
  {

$result127 = mysqli_query($con,"SELECT username FROM users WHERE id = '$row73[derguesi]'");
$row127 = mysqli_fetch_array($result127);
?>
<table width="320px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150" align="left"><font color="white" size="4"><b><?php echo $row127['username']; ?></b></font></td>
    <td width="170" align="right"><?php echo $row73['ora']; ?></td>
  </tr>
  <tr>
    <td colspan="2">
<font color="white"><?php echo $row73['mesazhi']; ?></font>
</td>
  </tr>
</table>
<?php } ?>
</div>
</td>
    <td width="150" height="450px" rowspan="2" valign="top" style=" padding:3px;  border-left:1px solid #FFFFFF;">
    <div style="width:150px; height:450px; overflow-y:auto;">
    <font color="#FFFFFF" size="+2"><b>Online users:</b></font><br /><br />
    <?php
        $result = mysqli_query($con,"SELECT username FROM users WHERE verifikuar='po' AND online = 'po'");
    while($row = mysqli_fetch_array($result))
          { ?>
                <font color="#FFFFFF"><b><?php echo $row['username']; ?></b></font>  <br />
          <?php }
        ?>
    </div>
    </td>
  </tr>
  <tr>
    <td height="50px" valign="middle" style="border-top:1px solid #FFFFFF; padding:3px; border-right:1px solid #FFFFFF;">
<form action="" method="post" name="comesazh">
<textarea name="mesazhi" cols="35" rows="4" required="required"></textarea>
<br /><input type="submit" name="submit654" id="submit654" value="Send" style="border-radius:0px; border-size:1px; border-style:solid; border-color:#ffffff; border-width:thin; background-color:#000000; color:#ffffff; height:26px; width:60px;  font-size:16px;" />
</form>
</td></tr>
</table>

</body>
</html>

我想知道是否可以使用 ajax 重新加载该页面以创建实时聊天,如果可以,该怎么做。
我尝试了很多方法,比如使用函数重新加载它的一部分,$.load但它不起作用。然后我尝试了$.load所有页面,但仍然没有工作。经过很多愚蠢的努力,我放弃了。

4

1 回答 1

5
$text = preg_replace('/(?<!\s)<a/', ' <a', $text);

应该做的伎俩

(?<!\s)后面称为负号查找,只有<a前面没有空格的才会被替换。

于 2013-06-27T18:08:14.827 回答