PHP replaces the template variable specified in the text paragraph (usage of str_replace)

Project requirements

Replace the fixed format {1}, {2}, {3}... in the text with the input form.

For example: To replace the fixed format {marketing rate}, {host manager} and {marketing counter number} in the text with the HTML of the input form,

$text = "就是签订合同的各方,大部分的合同主体是两方,一般写成甲方、乙方。根据合同的性质,有的合同也有别的写法,{营销利率}比如租赁合同写成出租方和租赁方,{营销柜号}承包合同写成发包人和承包人。合同主体,是个人的,一定要写身份证上的名字,是单位的,要写准确的全称,一个字也不要差,有营业执照的要写营业执照上的名称。最好是把身份证号、组织机构代码、住址、联系方式也写上{主办经理}。 ";

encapsulated function

/*
 * $text,文本内容
 * $labelText,标签文本
 * */
function getContactContent($text, $labelText)
{
    
    
    $replaceField = array();
    $replacement = array();
    foreach ($labelText as $k => $v) {
    
    
        $replacement[] = "<input type='text' class='lock-input' name='lockField' id='" . $k . "' />";
        $replaceField[] = '{' . $v . '}';
    }
    return str_replace($replaceField, $replacement, $text);
}

Effect test

$text = "就是签订合同的各方,大部分的合同主体是两方,一般写成甲方、乙方。根据合同的性质,有的合同也有别的写法,{营销利率}比如租赁合同写成出租方和租赁方,{营销柜号}承包合同写成发包人和承包人。合同主体,是个人的,一定要写身份证上的名字,是单位的,要写准确的全称,一个字也不要差,有营业执照的要写营业执照上的名称。最好是把身份证号、组织机构代码、住址、联系方式也写上{主办经理}。 ";
$labelText = array('营销利率', '营销柜号', '主办经理');
$analysisText = array('2%', '400万', '2024年3月23日');

echo getContactContent($text, $labelText);
echo "<hr>";
echo getContactAnalysis($text, $labelText, $analysisText);
echo "<hr>";
echo $analysisText[0];

Edit echo

/*
 * $text,文本内容
 * $labelText,标签文本
 * $analysisText,实际填写文本
 * */

function getContactAnalysis($text, $labelText, $analysisText)
{
    
    
    $replaceField = array();
    $replacement = array();
    foreach ($labelText as $k => $v) {
    
    
        $replacement[] = "<input class='lock-input-span' id='filed" . $k . "' value='$analysisText[$k]' οnclick='getFocus($k)'>";
        $replaceField[] = "{" . $v . "}";
    }
    return str_replace($replaceField, $replacement, $text);
}

Usage of str_replace

In PHP, str_replace()functions are used to replace something in a string. Its syntax is as follows:

str_replace(search, replace, subject)

Parameter Description:

  • search: The string or array of strings to find.
  • replace: The string or array of strings to replace.
  • subject: The target string or string array to be replaced.

Here is str_replace()an example using the function:

$text = "Hello, World!";
$newText = str_replace("World", "PHP", $text);
echo $newText;  // 输出:Hello, PHP!

In the example above, str_replace()the function $textreplaces "World" in the string with "PHP".

You can also use arrays as arguments to perform batch replacements. For example:

$text = "Hello, World!";
$search = array("World", "PHP");
$replace = array("Universe", "Laravel");
$newText = str_replace($search, $replace, $text);
echo $newText;  // 输出:Hello, Universe!

In the example above, str_replace()the function $textreplaces "World" with "Universe" and "PHP" with "Laravel" in the string.


@missingsometimes

Guess you like

Origin blog.csdn.net/weixin_41290949/article/details/135443429