我正在开发一个新闻网站,并允许任何文章的作者决定他们想在文章中添加多少图片。
要包含他们在文章内容正文中输入[image1]所需的图像,然后将其转换为另一页面上的图像src。
我试图编写一段代码来检查用户输入的图像数量以及附带的标题,然后查看是否已为每个图像应用了[image#]和[caption#]标记。
所需功能
如果用户选择为文章上传两张图片,则他们还必须插入两个字幕。然后,他们必须在文章的内容中的任何地方陈述[image1] [caption1]和[image2] [caption2]。
如果用户已将图片和字幕发布到表单中,但是没有包含正确数量的[图像#]和[字幕#],我希望使用{{来提示他们这样做1}}。然而,我正在努力实现这一目标。
到目前为止,这是我的代码,如果有人能告诉我我做错了什么,我们将不胜感激。
干杯,富有
strpos()
然后我只想简单地使用C& P来计算[image#]标签。
答案 0 :(得分:2)
如果预期它总是一个连续的字符串(如[image1] [caption1]),你可以这样试试:
<?php
$articleContent = 'The M-209 is a portable, mechanical cipher machine.
[image1][caption1] In this photograph, an intermediate gear unit meshes
with gears adjoining each key wheel.
[image2][caption2]Visible to the left of the image is the typewheel
that prints out messages and ciphertext onto paper tape.';
// Is something out of place or any captions missing?
preg_match_all( '~(\[image([\d])+\](?!\s*\[caption\\2\]))~i', $articleContent, $matches );
empty( $matches[0] ) or die( 'Please add proper caption to: ' . implode( ', ', $matches[0] ). '.' );
?>
如果在两个相应的标签之间应该是一堆东西,(比如[image1] blah 123 yeah [caption1])请使用这个正则表达式:
<?php
preg_match_all( '~(\[image([\d]+)\](?!.*\[caption\\2\]))~i', $articleContent, $matches );
?>
这里有一个不同的方法,没有丢失字幕编号报告......
<?php
$articleContent = 'The M-209 is a portable, mechanical cipher
machine used primarily by the US military in World War II, though
it remained in active use through the Korean War.
[image1][caption1] In this photograph, an intermediate gear unit
meshes with gears adjoining each key wheel.
[image2][caption2] Visible to the left of the image is the typewheel
that prints out messages and ciphertext onto paper tape.';
preg_match_all( '~(\[image[0-9]+\])~i', $articleContent, $images_array );
if( !empty( $images_array[0] ) ) {
preg_match_all( '~(\[caption[0-9]+\])~i', $articleContent, $captions_array );
if( count( array_unique( $images_array[0] ) ) > count( array_unique( $captions_array[0] ) ) ) {
echo 'You have not added all of your [caption#] tags in the article content.';
exit;
} else {
echo 'You have added all of your caption tags in the article.';
}
}
?>
答案 1 :(得分:2)
这可能有点矫枉过正,但希望它能给你一些有用的东西。
<?php
$article_content = "abc[image1]def[image2]ghi[image5]jkl[image123]mno" .
"[imagex]pqr[image3stu[image10]vwximage4]yza[caption3]bcd[caption1]" .
"efg[caption2]hij[caption5]klm[caption123]nop[caption10]qrs[image100]";
function find_tags($content, $tag_name) {
$tag_length = strlen($tag_name);
$ids = [];
$position = 0;
while(($position = strpos($content, "[" . $tag_name, $position)) !== false) {
$end_position = strpos($content, "]", $position);
if(is_numeric($id = substr($content, $position + $tag_length + 1, $end_position - $position - $tag_length - 1))) {
$ids[] = $id;
}
$position++;
}
return $ids;
}
function match_ids($tag_ids) {
$tag_names = array_keys($tag_ids);
sort($tag_ids[$tag_names[0]]);
sort($tag_ids[$tag_names[1]]);
$missing_ids_in_0 = array_diff(
$tag_ids[$tag_names[1]],
$tag_ids[$tag_names[0]]
);
$missing_ids_in_1 = array_diff(
$tag_ids[$tag_names[0]],
$tag_ids[$tag_names[1]]
);
return array(
$tag_names[0] . "_missing" => $missing_ids_in_0,
$tag_names[1] . "_missing" => $missing_ids_in_1
);
}
$tag_ids = array(
"image" => find_tags($article_content, "image"),
"caption" => find_tags($article_content, "caption")
);
var_dump($tag_ids);
$match_result = match_ids($tag_ids);
var_dump($match_result);
echo "<br>";
if(sizeof($match_result["image_missing"])) {
foreach($match_result["image_missing"] as $id) {
echo "Please add an [image" . $id . "] tag.<br>";
}
}
if(sizeof($match_result["caption_missing"])) {
foreach($match_result["caption_missing"] as $id) {
echo "Please add a [caption" . $id . "] tag.<br>";
}
}
?>
输出:
array (size=2)
'image' =>
array (size=6)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '5' (length=1)
3 => string '123' (length=3)
4 => string '10' (length=2)
5 => string '100' (length=3)
'caption' =>
array (size=6)
0 => string '3' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '5' (length=1)
4 => string '123' (length=3)
5 => string '10' (length=2)
array (size=2)
'image_missing' =>
array (size=1)
2 => string '3' (length=1)
'caption_missing' =>
array (size=1)
4 => string '100' (length=3)
Please add an [image3] tag.
Please add a [caption100] tag.
答案 2 :(得分:0)
因此,经过一周的尝试解决这个问题,我认为我最好分享答案,因为似乎缺乏人们试图解决这个问题。
<强>解决方案:强>
$articleContent = $_POST['articleContent'];
$imgCount = count($imgTmp1); // Count files array
$capCount = count($captions); // Count captions array
$r = 0;
$c = 0;
while($r < $imgCount){
$i = 1;
$z = 1;
$r++;
$c++;
$value = "[image$r]";
for($check = strpos($articleContent,$value);$i<$imgCount;$i++){
}
if($check !== false){
// Your success, if any
} else {
echo "<span class='error'>You need to include '$value' into the article content.</span>";
exit();
}
$value2 = "[caption$c]";
for($check2 = strpos($articleContent,$value2);$z<$capCount;$z++){
}
if($check2 !== false){
// Your success, if any
} else {
echo "<span class='error'>You need to include '$value2' into the article content.</span>";
exit();
}
}
问题不在于变量本身,因为它们在我尝试检查的每个循环中正确回显。问题实际上是strpos
。正如您所看到的,我已将strpos
插入循环本身,它就像桃子一样。
祝所有其他人在这里遇到同样的问题,祝他们好运,感谢其他人的帮助。