![](/img/trans.png)
[英]PHP rtrim() or similar function for removing X amount of characters from string
[英]Insert String from Array after X amount of characters (Outside HTML) in PHP
我已经看过,但找不到我们要编写的此功能的解决方案。 我对PHP还是很陌生,因此总是非常感谢任何帮助,建议和代码示例。
让我解释一下我们想做什么...
我们在字符串内有一个HTML块-该HTML内容字符串中包含的内容最多可以包含2000个单词,且带有样式,例如<p>
, <ul>
和<h2>
。
在单独的字符串中,我们还具有与此内容相关的图像数组。
我们需要将数组字符串中的图像以相等的间隔添加到HTML内容中,而不会破坏HTML代码。 因此,简单的字符计数将无法工作,因为它可能会破坏HTML标签。
我们需要等距排列图像。 因此,例如; 如果HTML内容字符串中有2000个单词,数组中有10个图像,则需要每200个单词放置一个图像。
为实现此目的而提供的任何帮助或编码示例都将不胜感激-提前感谢您的帮助。
您可以使用
$numword = str_word_count($str, 0);
获取行数
要么
$array = str_word_count($str,1);
为了在$ 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); ?>
这是相关的结果
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' >";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.