在段落标签之前插入多个字符串

时间:2018-12-15 02:43:17

标签: php string

我想在每个段落之前插入字符串,直到内容结尾。 如何不总是插入“版权A”?并在达到“版权C”限制时再次重置为开始

$text = array('Coypright A', 'Coypright B', 'Coypright C');
$limit = '3';

for ( $x = 0; $x < $limit; $x++ ){
  $str = str_replace("<p>", "$text[0]<p>", get_the_content());
}
echo $str;

3 个答案:

答案 0 :(得分:1)

您可以使用preg_replace_callback做您想做的事。在此示例代码中,回调依次遍历$text中的值,并在到达$limit时循环循环(使用$i = ($i + 1) % $limit;)。然后,将这些值放在文本中找到的任何<p>之前:

$text = array('Copyright A', 'Copyright B', 'Copyright C');
$limit = 3;
$content = '<p>Paragraph1</p>
<p>Paragraph2</p>
<p>Paragraph3</p>
<p>Paragraph4</p>
<p>Paragraph5</p>
';
echo preg_replace_callback('/<p>/', 
    function ($m) use ($text, $limit) { 
        static $i = -1; 
        $i = ($i + 1) % $limit; 
        return $text[$i] . $m[0]; 
    }, $content);

输出:

Copyright A<p>Paragraph1</p> 
Copyright B<p>Paragraph2</p> 
Copyright C<p>Paragraph3</p> 
Copyright A<p>Paragraph4</p> 
Copyright B<p>Paragraph5</p>

Demo on 3v4l.org

答案 1 :(得分:1)

如果您正在寻找一个简单的解决方案,则可以避免使用正则表达式,因为目标p标记是静态字符串。

但实际上,从长远来看,我建议使用domdocument的更强大的解决方案。

代码:(Demo

$text = array('Copyright A', 'Copyright B', 'Copyright C');
$limit = 3;
$content = '<p>Paragraph1</p>
<p>Paragraph2</p>
<p>Paragraph3</p>
<p>Paragraph4</p>
<p>Paragraph5</p>
';
$target = '<p>';
$ptags = explode($target, $content);
$new_content = array_shift($ptags);
foreach ($ptags as $i => $p) {
    $new_content .= $text[$i % $limit] . $target . $p;
}
var_export($new_content);

输出:

'Copyright A<p>Paragraph1</p>
Copyright B<p>Paragraph2</p>
Copyright C<p>Paragraph3</p>
Copyright A<p>Paragraph4</p>
Copyright B<p>Paragraph5</p>
'

答案 2 :(得分:0)

您不能重复Dim query922 = "CREATE TABLE D1 AS (SELECT q1.ID,q1.ItemName,q1.Purchase,q1.Sale, CASE WHEN @itemname=q1.ItemName THEN @runtot := @runtot + (q1.Purchase + q1.Sale) ELSE @runtot:=(q1.Purchase + q1.Sale)End AS ClosingBalance, @itemname:=q1.ItemName as Item FROM (SELECT ID AS ID,ItemName AS ItemName,Purchase AS Purchase,Sale AS Sale FROM itemledger ORDER BY ItemName) AS q1) " On Error Resume Next Using connection922 As New MySqlConnection(connectionString922) Dim command922 As New MySqlCommand(query922, connection922) command922.Connection.Open() command922.ExecuteScalar() command922.Connection.Close() End Using 次;在这种情况下,它只会执行3次循环。您需要迭代Dim query922 = "CREATE TABLE D1 AS (SELECT q1.ID,q1.ItemName,q1.Purchase,q1.Sale, CASE WHEN @itemname=q1.ItemName THEN @runtot := @runtot + (q1.Purchase + q1.Sale) ELSE @runtot:=(q1.Purchase + q1.Sale)End AS ClosingBalance, @itemname:=q1.ItemName as Item FROM (SELECT ID AS ID,ItemName AS ItemName,Purchase AS Purchase,Sale AS Sale FROM itemledger ORDER BY ItemName) AS q1) " On Error Resume Next Using connection922 As New MySqlConnection(connectionString922) Dim command922 As New MySqlCommand(query922, connection922) command922.Parameters.AddWithValue("@runtot", 0) command922.Parameters.AddWithValue("@itemname", "NULL") command922.Connection.Open() command922.ExecuteScalar() command922.Connection.Close() End Using 标签次数。发布代码后,无法知道有多少个p标签。要循环使用“版权”,请使用模数$limit运算符。

使用伪ish-code:

<p>

%循环。底线:您需要跟踪已处理了多少for ($i = 0; $i < "number of p tags"; $i++) { $str = str_replace("<p>", "$text[$i % $limit]<p>", get_the_content()); } 才能知道要“分配”哪个while