【问题标题】:php insert string into html after 500 words? include html tagsphp 在 500 个单词后将字符串插入到 html 中?包含html标签
【发布时间】:2014-10-29 16:08:57
【问题描述】:

如何在 500 字后插入一些字符串到 html 中? (不要破坏 html 标签)

function insert_string($text,$length,$insert) {
    preg_match_all('/<[^>]+>([^<]*)/',$text,$m,PREG_OFFSET_CAPTURE|PREG_SET_ORDER);
    foreach($m as $i=>$o){
        if($o[0][1]-$i>=$length)
        break;
        $t=substr(strtok($o[0][0]," \t\n\r\0\x0B>"),1);
        // ... can not think how to write continue.
    }
    //...
    return $output;
}


echo insert_string($text,'500','<strong> related tags </stong>');

应该是正则表达式每个html标签进入循环,记住一定的位置和strlen()的文本长度,然后确定在哪里插入一些刺。但我想不出怎么写继续。需要帮助。谢谢。

【问题讨论】:

  • 你认为一个词是什么?有什么用空格隔开的吗?
  • @Fede,简单的代码解释,像这样echo insert_string('&lt;div id="mw-content-text"&gt;&lt;p&gt;Lorem ipsum&lt;/p&gt;&lt;p&gt;dolor sit&lt;/p&gt;&lt;p&gt;amet&lt;/p&gt;&lt;/div&gt;',10,"&lt;stong&gt; related news &lt;/stong&gt;"); => &lt;div id="mw-content-text"&gt;&lt;p&gt;Lorem ipsum&lt;/p&gt;&lt;p&gt;dolor &lt;stong&gt; related news &lt;/stong&gt; sit&lt;/p&gt;&lt;p&gt;amet&lt;/p&gt;&lt;/div&gt;,应该是不打断html标签,应该考虑不打断一个字。

标签: php html regex insert


【解决方案1】:

我刚刚发现我曾经遇到过或多或少类似的问题。我修改了我的编码,所以它(希望)解决了你的问题。 它并不完美,但对我有用:

//start 'config
$string = '<div id="mw-content-text"><p>Lorem ipsum</p><p><b>dolor</b> sit</p><p>amet</p></div>';
$words = array();
$insert_after_x_words = 3;
$insert_string = '<br>INSERT YOUR STUFF HERE<br>';
//end 'config'

$needle = '<';
$positions_tagopen = array();
$lastPos = 0;
while (($lastPos = strpos($string, $needle, $lastPos))!== false) {
  array_push($positions_tagopen, $lastPos);
  $lastPos = $lastPos + strlen($needle);
}

$needle = '>';
$positions_tagclose = array();
$lastPos = 0;
while (($lastPos = strpos($string, $needle, $lastPos))!== false) {
  array_push($positions_tagclose, $lastPos);
  $lastPos = $lastPos + strlen($needle);
}


$current_pos = 0;
for($i = 0; $i < count($positions_tagopen); $i++)
{
  $current_substring = substr($string,$current_pos,$positions_tagopen[$i]-$current_pos);
  $tmp = explode(" ",$current_substring);
  for($i2 = 0; $i2 < count($tmp); $i2++)
  {
    if(strlen($tmp[$i2])>0)
    {
      if($i2+1<count($tmp))
      {
        $tmp[$i2] = $tmp[$i2]." ";
      }
      array_push($words, $tmp[$i2]);                
    }
  }

  array_push($words, html_escape(substr($string,$positions_tagopen[$i], $positions_tagclose[$i]-$positions_tagopen[$i]).'>'));
  $current_pos = $positions_tagclose[$i]+1;
}

for($i = 0; $i < count($words); $i++)
{
  if(($i)%$insert_after_x_words == 0 && $i > 0)
  {
    echo $insert_string;
  }
  echo $words[$i];
}

【讨论】:

  • 致命错误:调用未定义的函数 html_escape()。
  • 一个 HTML-Tag 被认为是“一个词”。
  • 您使用的是旧版本的 PHP。但是,这仅用于调试,您可以将其删除。因为如果您的字符串包含类似

    (或其他 HTML 标记)之类的内容,它将在调试输出中创建一个新段落(结果仍然是正确的)

  • html_escape() 的手册在哪里?我在 php.net 中找不到它
  • 您也可以使用htmlentities()或类似功能。正如我所说,我只使用它来转义 html 标签,因此调试(!)输出在您的网络浏览器中显示时是可读的。我不知道为什么这个函数在我的 PHP 发行版中可用(它是一个普通的 XAMPP-Server)
猜你喜欢
  • 2016-01-01
  • 2019-10-17
  • 1970-01-01
  • 2021-09-29
  • 2020-07-10
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多