3

データベースからのコンテンツがいくつかあります。そして、コンテンツの特定の単語を一連のコードに置き換えたいと思います。

データベースからのコンテンツは次のとおりです。

当ウェブサイトにご関心をお寄せいただき、誠にありがとうございます。
{FORMINSERT}
1234567890までお電話いただくこともできます

{FORMINSERT}文字列を一連のPHPコードに置き換えたいと思います。通常のテキスト文字列の場合は、を使用して簡単に置き換えることができますstr_replace

ただし、置換するコンテンツは単純なテキストではなく、フォームコードです。

これを交換したい{FORMINSERT}

例:

<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
    <table cellpadding="5" cellspacing="2" >
        <tr>
            <td width="84" ><a name="contact" id="contact"></a></td>
            <td width="384">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="2" ><h1>Contact Us</h1></td>
        </tr>
        <tr>
            <td ><label for="fullname">Name:</label></td>
            <td>
                <input type="text" name="fullname" id="fullname" value="<?php echo KT_escapeAttribute($row_rsscotts_contact['fullname']); ?>" size="47" />
                <?php echo $tNGs->displayFieldHint("fullname");?> <?php echo $tNGs->displayFieldError("scotts_contact", "fullname"); ?>
            </td>
        </tr>
        <tr>
            <td ><label for="phone">Phone:</label></td>
            <td>
                <input type="text" name="phone" id="phone" value="<?php echo KT_escapeAttribute($row_rsscotts_contact['phone']); ?>" size="47" />
                <?php echo $tNGs->displayFieldHint("phone");?> <?php echo $tNGs->displayFieldError("scotts_contact", "phone"); ?>
            </td>
        </tr>
        <tr>
            <td><label for="email">Email:</label></td>
            <td>
                <input type="text" name="email" id="email" value="<?php echo KT_escapeAttribute($row_rsscotts_contact['email']); ?>" size="47" />
                <?php //echo $tNGs->displayFieldHint("email");?> <?php echo $tNGs->displayFieldError("scotts_contact", "email"); ?>
            </td>
        </tr>
        <tr>
            <td><label for="tellus">Looking for:</label></td>
            <td>
                <textarea name="tellus" id="tellus" cols="37" rows="5"><?php echo KT_escapeAttribute($row_rsscotts_contact['tellus']); ?></textarea>
                <?php echo $tNGs->displayFieldHint("tellus");?> <?php echo $tNGs->displayFieldError("scotts_contact", "tellus"); ?>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="submit" name="KT_Insert1" id="KT_Insert1" value="Submit" class="button-blue" /> 
                <input name="Reset" type="reset" value="Reset" class="button-grey" />
            </td>
        </tr>
    </table>
</form>
4

5 に答える 5

2

test1.php:

$database_content = 'Thank you for interest on our web site.
{FORMINSERT}
You can also contact us by calling us to 1234567890';

if(stripos($database_content, '{FORMINSERT}') !== FALSE){
    ob_start();
    include 'test2.php';
    $result = ob_get_clean();
}

$database_content = str_replace("{FORMINSERT}", $result, $database_content);

echo $database_content;

test2.php(挿入しようとしているコード):

echo 'hello world';

結果:

当ウェブサイトにご関心をお寄せいただき、誠にありがとうございます。helloworld1234567890までお電話いただくこともできます

つまり、コード「echo'helloworld';」のようになります。{FORMINSERT}があった場所に座っていました。そのように含めるPHPファイルの束を作成し、置換を処理するためにいくつかのifステートメントを作成することができます。

于 2012-12-16T13:00:54.240 に答える
2

ob_start()HTML と PHP コードを組み合わせて、出力を 1 つの変数内に保存する場合に使用できます。

ob_start();
?>
    <form action="contact.php" method="post">
    Few fields here
    and submit button
    </form>
<?php

$forminsert = ob_get_clean();

str_replace()その後、通常どおりに行うことができます。

ただし、が表示される場合と表示されない場合がある場合は、必要がない場合にフォーム データを生成するコストを削減するために{FORMINSERT}使用できます。preg_replace_callback()

$content = preg_replace_callback('/{(.*?)}/', function($match) {
    if ($match[1] == 'FORMINSERT') {
        // code to generate $forminsert
        return $forminsert;
    }
    return $match[0];
}, $content_from_db);

ところで、この関数は、中括弧の間のものを置き換えるために、より一般的に使用することもできます。

于 2012-12-16T12:54:08.153 に答える
0

これを行うだけであなたの仕事は解決されます

       $forminser= " welcome to our website {FORMINSERT}";
       $form= "<form action='contact.php' method='post'>
                 Few fields here
                and submit button
              </form>" ;
       echo str_replace("{FORMINSERT}",$form,$forminser);

編集>フォーム内にphpコードが必要な場合は、ここに例を示します

     $var = "Few words here" ;
     $forminser= " welcome to our website {FORMINSERT}";
     $form= "<form action='contact.php' method='post'>";
     $form .= $var ;  // this php code here
     $form .= "  and submit button</form>" ;
         echo str_replace("{FORMINSERT}",$form,$forminser);

edit2

ここに、zouが作成できるコード全体があります。

    <?php

 $forminser= " welcome to our website {FORMINSERT}";
 $form = "<form method='post' id='form1' action=' " ;
 $form .=  KT_escapeAttribute(KT_getFullUri()); 
 $form .= " '><table cellpadding='5' cellspacing='2' >
         <tr>
         <td width='84' ><a name='contact' id='contact'></a></td>
         <td width='384'>&nbsp;</td>
         </tr>
         <tr>
         <td colspan='2' ><h1>Contact Us</h1></td>
         </tr>
          <tr>
          <td ><label for='fullname'>Name:</label></td>
          <td><input type='text' name='fullname' id='fullname' value=' " ;

$form .= KT_escapeAttribute($row_rsscotts_contact['fullname']); 
$form .= " ' size='47' /> ";
$form .= $tNGs->displayFieldHint("fullname");
$form .= $tNGs->displayFieldError("scotts_contact", "fullname"); 
$form .= "</td>
         </tr>
         <tr>
         <td ><label for='phone'>Phone:</label></td>
         <td><input type='text' name='phone' id='phone' value= ' " ;
$form .= KT_escapeAttribute($row_rsscotts_contact['phone']);
$form .= " ' size='47' /> ";
$form .= $tNGs->displayFieldHint("phone");
$form .= $tNGs->displayFieldError("scotts_contact", "phone"); 
$form .= '</td>
         </tr>
        <tr>
        <td><label for="email">Email:</label></td>
        <td><input type="text" name="email" id="email" value=" ' ;
$form .= KT_escapeAttribute($row_rsscotts_contact['email']);
$form .= '" size="47" />';
$form .=  $tNGs->displayFieldError("scotts_contact", "email"); 
$form .= '</td>
         </tr>
         <tr>
         <td><label for="tellus">Looking for:</label></td>
          <td><textarea name="tellus" id="tellus" cols="37" rows="5"> ';
$form .=  KT_escapeAttribute($row_rsscotts_contact['tellus']); 
$form .= '</textarea> ';
$form .= $tNGs->displayFieldHint("tellus");
$form .=  $tNGs->displayFieldError("scotts_contact", "tellus"); 
$form .= '</td>
        </tr>
        <tr>
        <td></td>
         <td><input type="submit" name="KT_Insert1" id="KT_Insert1" value="Submit"  class="button-blue" /> <input name="Reset" type="reset" value="Reset" class="button-grey" /></td>
       </tr>
     </table>
    </form>
     ';

   echo str_replace("{FORMINSERT}",$form,$forminser);
 ?>         
于 2012-12-16T13:02:40.000 に答える
0

さて、これはどうですか。以下のコードを使用すると、コンテンツ内にカスタム{words}を定義でき、解析時に他のコンテンツに置き換えられます。

$contentFromDB = "Etiam porta sem malesuada magna mollis euismod. Donec ullamcorper nulla non metus auctor fringilla. {FORMINSERT}";

echo matchTags($contentFromDB);


function matchTags($content) {
   $pattern = '/{(\w+)}/i';
   $content = preg_replace_callback($pattern,"transformTags",$content);
   return $content;
}


function transformTags($word) {

    if ($word[1] == "FORMINSERT") {

        ob_start();
        ?>
        <form action="contact.php" method="post">
        Few fields here
            and submit button
        </form>
        <?php 
        $content = ob_get_clean();
        return $content;
    }

    if ($word[1] == "somethingelse") {

    }

}
于 2012-12-16T13:09:35.140 に答える
0

あなたが持っているデータベースからのコンテンツが変数に保存されていると仮定しましょう$db_content& あなたが置き換える必要があるphpコードはcustom_code.phpそうです

if(strpos($db_content, "{FORMINSERT}") === true){
    //remove the tag
    str_replace("{FORMINSERT}", '',$db_content)
    //load the php code
   require_once("custom_code.php")
}
//if need, you can add more conditions using else-if & replace more tags. 

MVCただし、テンプレートを使用してそのような作業を簡単に実行できるパターンに移行することをお勧めします。これは、単純なスクリプトに使用する例です

于 2012-12-16T12:54:00.617 に答える