检查一个字符串是否包含多段文本
Checking a string contains multiple pieces of text
我正在开发一个新闻网站,并允许任何文章的作者决定他们想在文章中添加多少图片。
要在文章内容的 body 中包含他们需要输入 [image1] 的图像,然后将其转换为另一个页面上的图像 src。
我正在尝试编写一段代码来检查用户输入了多少图像以及随附的标题,然后查看他们是否为每个图像应用了 [image#] 和 [caption#] 标签。
所需功能:
如果用户选择为文章上传两张图片,他们还必须插入两个标题。然后,他们必须在文章内容中的任意位置声明 [image1][caption1] 和 [image2][caption2]。
如果用户已将图片和标题发布到表单中,但没有包含正确数量的 [image#] 和 [caption#],我希望使用 [= 提示他们这样做11=]。然而我正在努力实现这一目标。
这是我到目前为止的代码,如果有人能告诉我我做错了什么,我们将不胜感激。
干杯,里奇
$z = 1;
while($z < $caption){
if (strpos($articleContent,'[caption$z]') == !false) {
echo 'You have added all of your caption tags in the article.';
$z++;
} else {
echo "You have not added all of your [caption#] tags in the article content.";
exit();
}
}
然后我希望简单地 C&P 相同来计算 [image#] 标签。
如果它总是一个连续的字符串(如[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.';
}
}
?>
这可能有点矫枉过正,但希望它能给你一些有用的东西。
<?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.
所以在尝试解决这个问题一周之后,我想我最好分享答案,因为似乎没有人尝试这样做。
解法:
$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
,它就像桃子一样工作。
祝其他遇到同样问题的人好运,也感谢其他人的帮助。
我正在开发一个新闻网站,并允许任何文章的作者决定他们想在文章中添加多少图片。
要在文章内容的 body 中包含他们需要输入 [image1] 的图像,然后将其转换为另一个页面上的图像 src。
我正在尝试编写一段代码来检查用户输入了多少图像以及随附的标题,然后查看他们是否为每个图像应用了 [image#] 和 [caption#] 标签。
所需功能:
如果用户选择为文章上传两张图片,他们还必须插入两个标题。然后,他们必须在文章内容中的任意位置声明 [image1][caption1] 和 [image2][caption2]。
如果用户已将图片和标题发布到表单中,但没有包含正确数量的 [image#] 和 [caption#],我希望使用 [= 提示他们这样做11=]。然而我正在努力实现这一目标。
这是我到目前为止的代码,如果有人能告诉我我做错了什么,我们将不胜感激。
干杯,里奇
$z = 1;
while($z < $caption){
if (strpos($articleContent,'[caption$z]') == !false) {
echo 'You have added all of your caption tags in the article.';
$z++;
} else {
echo "You have not added all of your [caption#] tags in the article content.";
exit();
}
}
然后我希望简单地 C&P 相同来计算 [image#] 标签。
如果它总是一个连续的字符串(如[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.';
}
}
?>
这可能有点矫枉过正,但希望它能给你一些有用的东西。
<?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.
所以在尝试解决这个问题一周之后,我想我最好分享答案,因为似乎没有人尝试这样做。
解法:
$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
,它就像桃子一样工作。
祝其他遇到同样问题的人好运,也感谢其他人的帮助。