在PHP中,将数组中的字符串插入到X个字符(HTML之外)之后
我已经看过了,但找不到我们想写的这个特性的解决方案。我对PHP相当陌生,所以任何帮助、建议和代码示例都非常感谢 让我解释一下我们想做什么 我们在一个字符串中有一块HTML-内容最多可以是2000个单词,样式为在PHP中,将数组中的字符串插入到X个字符(HTML之外)之后,php,arrays,string,character,Php,Arrays,String,Character,我已经看过了,但找不到我们想写的这个特性的解决方案。我对PHP相当陌生,所以任何帮助、建议和代码示例都非常感谢 让我解释一下我们想做什么 我们在一个字符串中有一块HTML-内容最多可以是2000个单词,样式为,,,包含在此HTML内容字符串中 我们在一个单独的字符串中还有一个与此内容相关的图像数组 我们需要在不破坏HTML代码的情况下,将数组字符串中的图像以相等的空间添加到HTML内容中。因此,一个简单的字符计数将不起作用,因为它可能会破坏HTML标记 我们需要将图像均匀地隔开。比如说,;如果H
,
,
,包含在此HTML内容字符串中
我们在一个单独的字符串中还有一个与此内容相关的图像数组
我们需要在不破坏HTML代码的情况下,将数组字符串中的图像以相等的空间添加到HTML内容中。因此,一个简单的字符计数将不起作用,因为它可能会破坏HTML标记
我们需要将图像均匀地隔开。比如说,;如果HTML内容字符串中有2000个单词,数组中有10个图像,那么我们需要每200个单词放置一个图像
非常感谢为实现此目的而提供的任何帮助或编码示例-提前感谢您的帮助。您可以使用
$numword = str_word_count($str, 0);
用于获取行数
或
要在$array中获得一个包含所有单词的数组(一个用于索引),然后在此数组上迭代重建文本,您需要为图像添加每一次(单词)代码
此示例是表单php手册
<?php
$str = "Hello fri3nd, you're
looking good today!";
print_r(str_word_count($str, 1));
print_r(str_word_count($str, 2));
print_r(str_word_count($str, 1, 'àáãç3'));
echo str_word_count($str);
?>
你可以在网上找到它
对于插入,您可以这样尝试
$num = 200; // number of word after which inert the image
$text = $array[0]; // initialize the text with the first word in array
for ($cnt =1; $cnt< count( $array); $cnt++){
$text .= $array[$cnt]; // adding the word to the text
if (($cnt % $num) == 0) { // if array index multiple fo 200 insert the image
$text .= "<img src='your_img_path' >";
}
}
$num=200;//在其后面显示图像的字数
$text=$array[0];//用数组中的第一个单词初始化文本
对于($cnt=1;$cnt
很抱歉我的无知,但是我们如何将数组中的图像插入到所需位置的HTML字符串中?我已经更新了答案,如果答案有用,请将其评级和/或标记为已接受…感谢回复。这不会破坏代码中的HTML吗?它会将HTML标记计算为单词吗?我们有- 标记,这样才能插入图像?您能发布/展示一个编辑您的问题的示例吗?。。。。
Array
(
[0] => Hello
[1] => fri
[2] => nd
[3] => you're
[4] => looking
[5] => good
[6] => today
)
Array
(
[0] => Hello
[6] => fri
[10] => nd
[14] => you're
[29] => looking
[46] => good
[51] => today
)
Array
(
[0] => Hello
[1] => fri3nd
[2] => you're
[3] => looking
[4] => good
[5] => today
)
7
$num = 200; // number of word after which inert the image
$text = $array[0]; // initialize the text with the first word in array
for ($cnt =1; $cnt< count( $array); $cnt++){
$text .= $array[$cnt]; // adding the word to the text
if (($cnt % $num) == 0) { // if array index multiple fo 200 insert the image
$text .= "<img src='your_img_path' >";
}
}