Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.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字符串添加到另一个HTML字符串中?_Php_Html_Simple Html Dom - Fatal编程技术网

PHP:如何在特定位置将一个HTML字符串添加到另一个HTML字符串中?

PHP:如何在特定位置将一个HTML字符串添加到另一个HTML字符串中?,php,html,simple-html-dom,Php,Html,Simple Html Dom,在我的PHP脚本中,我有来自另一个页面的str1(参见下面的代码),但为了简单起见,我在代码中复制了它的值 在字符串str1中,如果需要,我想使用simple\u html\u dom将str2添加到div#option\u one\u div之前 我该怎么做 <?php $str1 = '<div style="padding:25px;"> <div style="background-color:#d9b2b2;"> <labe

在我的PHP脚本中,我有来自另一个页面的
str1
(参见下面的代码),但为了简单起见,我在代码中复制了它的值

在字符串
str1
中,如果需要,我想使用
simple\u html\u dom
str2
添加到div
#option\u one\u div
之前

我该怎么做

<?php

$str1 = '<div style="padding:25px;">
    <div style="background-color:#d9b2b2;">
        <label>Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah </label>
    </div>
    <div id="option_one_div" style="background-color:#74d4dd;">
        <input style="margin-left:30px; margin-right:30px;" type="radio" name="radio" value="0">
        <input style="margin-left:30px; margin-right:30px;" type="radio" name="radio" value="1">
        <label style="margin-left:25px; margin-right:25px;" for="option_one_div">Label of first group of Radio Buttons</label>
    </div>
    <div id="option_two_div" style="background-color:#36d666;">
        <input style="margin-left:30px; margin-right:30px;" type="radio" name="radio" value="0">
        <input style="margin-left:30px; margin-right:30px;" type="radio" name="radio" value="1">
        <label style="margin-left:25px; margin-right:25px;" for="option_two_div">Label of second group of Radio Buttons</label>
    </div>
</div>';

$str2 = '
    <div style="background-color:#bf5b5b; ">
        <label style="margin-left:25px; margin-right:25px;">Yes</label>
        <label style="margin-left:25px; margin-right:25px;">No</label>
    </div>';

?>

如果字符串不变,则可以在插入之前插入字符串,这比分离html dom更容易

$find = '<div id="option_two_div"';
$html = str_replace($find,$str2.$find,$str1);

$find=”如果字符串不变,则可以在插入之前将其插入,而不是将html dom分开

$find = '<div id="option_two_div"';
$html = str_replace($find,$str2.$find,$str1);

$find='可能有更好的方法,但您可以使用str\u replace

$newstr = str_replace('<div id="option_one_div"', $str2.'<div id="option_one_div"', $str1);

$newstr=str_replace(“可能有更好的方法,但您可以使用str_replace

$newstr = str_replace('<div id="option_one_div"', $str2.'<div id="option_one_div"', $str1);

$newstr=str_用简单的html替换('),这看起来像:

$html = str_get_html($str1);
$option_one_div = $html->find('#option_one_div', 0);
$option_one_div->outertext = $str2 . $option_one_div;

使用简单的html do m,这看起来像:

$html = str_get_html($str1);
$option_one_div = $html->find('#option_one_div', 0);
$option_one_div->outertext = $str2 . $option_one_div;