duanjiao1256 2013-07-19 13:40
浏览 71
已采纳

如何在php字符串中将文本插入html标签?

i have been dealing with this for awhile and i could not figure out how to do the following in 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("
", $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 ";

how can i do this? Please note i cannot do

$string="some text here <pre> WHERE $insert1 SHOULD BE</pre> some more text here <pre>WHERE $insert2 SHOULD BE</pre> ok how";

because i am getting $insert1 and $insert2 from $string to modify. i need to place it back in there where they come from. thank you

  • 写回答

4条回答 默认 最新

  • dsgm5631 2013-07-19 13:42
    关注

    It's pretty easy to do:

    $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 ";
    

    You can echo a variable from inside a string when it's wrapped around double quotes. This does not work with single quotes.

    Edit: This might be what you are looking for then:

    $string = "This my <code>awesome</code> code";
    
    $string = preg_replace('/<code>(.*?)<\/code>/', '<pre>$1</pre>', $string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部