这里我在php中嵌入了html代码。
但是执行时会给出错误。我查了很多次但看不到bug。 帮助赞赏:请看
<?php
$url = $_POST['url'];
$user_id = $_POST ['userid'];
if(isset($_POST['rate']))
{
$rate =$_POST['rate'];
}
else
$rate = 0;
$data = file_get_contents($url);
function get_title($html)
{
return preg_match('!<title>(.*?)</title>!i', $html, $matches) ? $matches[1] : '';
}
function get_logo($html)
{
preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches);
//echo "mactch : $matches[0][0]";
return $matches[0][0];
}
function plaintext($html)
{
// remove comments and any content found in the the comment area (strip_tags only removes the actual tags).
$plaintext = preg_replace('#<!--.*?-->#s', '', $html);
// put a space between list items (strip_tags just removes the tags).
$plaintext = preg_replace('#</li>#', ' </li>', $plaintext);
// remove all script and style tags
$plaintext = preg_replace('#<(script|style)\b[^>]*>(.*?)</(script|style)>#is', "", $plaintext);
// remove br tags (missed by strip_tags)
$plaintext = preg_replace("#<br[^>]*?>#", " ", $plaintext);
// remove all remaining html
$plaintext = strip_tags($plaintext);
return $plaintext;
}
function trim_display($size,$string)
{
$trim_string = substr($string, 0, $size);
$trim_string = $trim_string . "...";
return $trim_string;
}
$title = get_title($data);
$logo = get_logo($data);
$title_display = trim_display(30,$title);
$content = plaintext($data);
$Preview = trim_display(100,$content); //to Show first 100 char of the web page as preview
function MakeTinyUrl($url)
{
return md5($url);
}
$hash = MakeTinyUrl($url);
ob_start();
$con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$content=mysqli_real_escape_string($con,$content);
$Preview=mysqli_real_escape_string($con,$Preview);
$title_display=mysqli_real_escape_string($con,$title_display);
$result = mysqli_query($con,"SELECT COUNT(*) as val FROM post_data WHERE url ='".$url."' and userid='".$user_id."'");
$bool= mysqli_fetch_assoc($result);
if($bool['val'])
{
echo '<div style="clear:both;"><i>You have already worked on this url..</i> </div>';
}
else
{
$insertQuery = "INSERT INTO post_data(`userid`, `url`, `desc`, `preview`, `img_url`, `title` ,`hash`) VALUES ('".$user_id."','".$url."','".$content."','".$Preview."','".$logo."','".$title_display."','".$hash."')";
if (!mysqli_query($con,$insertQuery))
{
die('Error: ' . mysqli_error($con));
}
}
$result = mysqli_query($con,"SELECT * FROM post_data WHERE userid ='".$user_id."' and url='".$url."'");
//This will fetch only one row from db
while ($row = @mysqli_fetch_array($result))
{
$title = $row['title'];
$url = $row['url'];
$preview = $row['preview'];
$image = $row['img_url'];
}
//Update Rate value in table
$update = "update post_data set rate='".$rate."' where url='".$url."'";
if (mysqli_query($con, $update))
{
//echo "updated";
}
else
{
//echo "Not updated";
}
echo '<style type="text/css">
.fragment
{
font-size: 12px;
font-family: tahoma;
height: 140px;
width: 400px;
border: 1px solid #ccc;
color: #555;
display: block;
padding: 10px;
box-sizing: border-box;
text-decoration: none;
}
.fragment:hover
{
box-shadow: 2px 2px 5px rgba(0,0,0,.2);
}
.fragment img
{
float: left;
margin-right: 10px;
}
.fragment h3
{
padding: 0;
margin: 0;
color: #369;
}
.fragment h4
{
padding: 0;
margin: 0;
color: #000;
}
.fragment tbox
{
padding: 1px;
height: 30px;
width: 424px;
}
#close
{
float:right;
display:inline-block;
padding:2px 5px;
background:#ccc;
}
</style>
<a class="fragment" href="google.com">
<div>
<span id="close" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;">x</span>';
echo '<img src =' $image echo '" height="116" width="116" alt="some description"/>';
echo '<h3>'; $title echo '</h3>';
echo '<h4>'; $url echo ' </h4>';
echo '<p class="text">'; $preview
echo '</p>
</div>
</a>';
$div1 = ob_get_clean();
ob_start();
$result = mysqli_query($con,"SELECT * FROM post_data WHERE userid ='".$user_id."'");
echo "Records for user : $user_id"; echo "<br/>";
while ($row = @mysqli_fetch_array($result))
{
$title = $row['title'];
$url = $row['url'];
$preview = $row['preview'];
$image = $row['img_url'];
echo "Title: $title"; echo '<br/>';
echo "URL: $url"; echo '<br/>';
echo "Preview : $preview"; echo '<br/>';
echo "Image url: $image"; echo '<br/>';
//echo '</br>';
}
$div2 = ob_get_clean();
$resultArray = array("resultOne" => $div1,"resultTwo" => $div2);
echo json_encode($resultArray);
?>
https://gist.github.com/karimkhanp/b8bfb88db6e438fb8be6
当我尝试获取json响应时,它会给出以下错误:
SyntaxError: JSON.parse: unexpected character
var data=JSON.parse(xmlhttp.responseText);
答案 0 :(得分:2)
使用字符串连接(.
)来连接字符串。
只需替换168-176行:
<a class="fragment" href="google.com">
<div>
<span id="close" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;">x</span>';
echo '<img src ='.$image.'" height="116" width="116" alt="some description"/>';
echo '<h3>'.$title.'</h3>';
echo '<h4>'.$url.'</h4>';
echo '<p class="text">'.$preview.'</p>
</div>
</a>';
我建议您使用更好的文本编辑器,如Notepad ++或Adobe Dreamweaver。