加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何捕获并将第二个粗体文本存储到数据库中?

发布时间:2020-12-13 22:23:09 所属栏目:PHP教程 来源:网络整理
导读:假设将整个字符串存储到数据库中,并将句子拆分,选项和答案存储在自己的列中. $example = ‘She went to the store to ( buy ,buys,bought,buying) some snacks when she (sees,see, saw ,look) the accident.’; 管理将整个字符串存储到数据库中并将它们存储
假设将整个字符串存储到数据库中,并将句子拆分,选项和答案存储在自己的列中.

$example = ‘She went to the store to (buy,buys,bought,buying) some snacks when she (sees,see,saw,look) the accident.’;

管理将整个字符串存储到数据库中并将它们存储到相应的列中,并能够捕获第一个粗体文本并将其存储到数据库中.

但是无法捕获并将第二个粗体文本存储到数据库的列中.

<?php 
define('DB_HOST','localhost');
define('DB_NAME','palmdatas'); 
define('DB_USER','root');
define('DB_PASSWORD',''); 

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error()); 
$mode = $_POST['mode']; 
$question = $_POST['editor1']; 
$text = $question;

echo "n";

$text = strip_tags($text);


function multiexplode ($delimiters,$string) {

$ready = str_replace($delimiters,$delimiters[0],$string);
$launch = explode($delimiters[0],$ready);
return  $launch;
}

$exploded = multiexplode(array(",","(",")"),$text);
 $question = $_POST['editor1']; 

 $answer1 = extractString($question,'<strong>','</strong>');

$sql="INSERT INTO strucquestions (part1,op1,op2,op3,op4,ans1,part2,op1a,op2a,op3a,op4a,ans2,part3,mode) VALUES ('$exploded[0]','$exploded[1]','$exploded[2]','$exploded[3]','$exploded[4]','$answer1','$exploded[5]','$exploded[6]','$exploded[7]','$exploded[8]','$exploded[9]','$answer2','$exploded[10]','$mode')";

echo "Successfully Inserted!";
$result = mysql_query($sql);
if (!$result) {
 die('Invalid query: ' . mysql_error());
}

function extractString($question,$start,$end) {
$question = " ".$question;
$ini = strpos($question,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($question,$end,$ini) - $ini;
return substr($question,$ini,$len);

echo "close";
        mysql_close($con);      

} 
?>
<button onclick="window.location.href='insertion.php'">Next</button><br>        <br>

解决方法

看来你想看看 http://php.net/manual/en/function.preg-match-all.php:

Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.

这可能比使用strpos业务更优雅,因为在使用它时你必须循环直到你到达输入字符串的末尾.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读