Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文本插入php字符串中的html标记?_Php_Html_Tags - Fatal编程技术网

如何将文本插入php字符串中的html标记?

如何将文本插入php字符串中的html标记?,php,html,tags,Php,Html,Tags,我已经处理这个问题有一段时间了,我不知道如何在PHP中执行以下操作: $string = "this is test <pre>somrthing in <p>pharagraph</p></pre> dont know tell me <pre>value33 kooo ok</pre> this is php regress <pre>teeth value</pre> ok how"; fu

我已经处理这个问题有一段时间了,我不知道如何在PHP中执行以下操作:

$string = "this is test <pre>somrthing in <p>pharagraph</p></pre> dont know tell me <pre>value33 kooo ok</pre> this is php regress <pre>teeth value</pre> ok how"; 

function get_innercode($string) {
preg_match_all("#<pre>(.*?)</pre>#", $string, $foo); 
echo implode("\n", $foo[1]); 
}
    $insert1=get_innercode($string);



    $insert2=" 2 place me in the pre tag"; // you can ignore this

    $string="this is test  <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me  <pre>WHERE $insert2 SHOULD BE</pre> ok how ";
我该怎么做? 请注意我做不到

$string_new = preg_replace_callback(
   '#<pre>(.*?)</pre>#',
    function ($matches) {
        return "<div>".$matches[0]."</div>";
    },
    $string
);

echo $string_new;
因为我将从$string中获得$insert1和$insert2以进行修改。我需要把它放回他们来的地方。
谢谢

这很容易做到:

“,$string;
在php中,可以将字符串与。前后,例如:


这里也有类似的方法。这允许填充更大的区域

分析您的内容行,如下所示:

这是未经测试的,因此可能有一些bug。

试试这个

我在每个pre内容之前添加div标记。您还可以使用$matches[1]根据需要修改内容


对于这样的简单变量,您甚至不需要大括号。@Barmar这是正确的,但在某些编辑器中,使用大括号可以使变量脱颖而出。谢谢,但我正在为$string获取$insert1和$insert2并对其进行修改。然后我想把它放回它们的来源。所以我不能手动定义并打印它。@Justin:那么你需要澄清一下,并展示一些真实的代码。因为你的问题代码是一个相当糟糕的例子;如前所述,它工作得很好。@Justin,这仍然没有阐明你想要完成什么。应该按原样工作。。。只需回显它`回显$string`这将起作用,fsvo-尽管它可能会对HTML注入开放,这取决于数据来自哪里。您遇到了什么问题?我相信问题是:如何提取元素中的文本?从中提取与插入不同。为了清晰起见,请更新问题/标题。$insert1和$insert2是从这些预标记中提取的。修改后我需要把它们放回那里。你能提供一个从操作开始到结束的预期代码的最小示例吗?这对我来说没有多大意义。正如从另一个答案复制过来的:正常的字符串插值,正如发布的一样,获得了相同的结果。因此,这不是改进或解决方案,尽管我仍然不确定实际问题是什么。
$insert1 =" 1 place me in the pre tag";
$insert2 =" 2 place me in the pre tag";

$string = "some text here <pre>{$insert1}</pre> some more text here <pre>{$insert2}</pre> and may be some more text or ";
$string = "This my <code>awesome</code> code";

$string = preg_replace('/<code>(.*?)<\/code>/', '<pre>$1</pre>', $string);
$var1 = "hello";
$var2 = "What's up";

$concat = "Hey, ".$var1." ,".$var2."<br>";

echo $concat; 
$insert[0] = "Some Text";
$insert[1] = "Some More Text";

function Parser($content){
$i = 0;
    while (preg_match_all('`\<pre\>(.*?)\</pre\>`', $content, $matches)) {
       foreach ($matches[0] as $key => $match) { 
         //$innertext = $matches[1][$key]; //replace with your preferred filler
         $innertext = $insert[$i]; //such as this
         $replacement = '<pre>' . trim($innertext) . '</pre>';
         $content = str_replace($match, $replacement, $content); 
         $i++;
       }
    }
}
Parser("this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me  <pre>WHERE $insert2 SHOULD BE</pre> ok how");
$string_new = preg_replace_callback(
   '#<pre>(.*?)</pre>#',
    function ($matches) {
        return "<div>".$matches[0]."</div>";
    },
    $string
);

echo $string_new;