How to insert a word through php between these tags?

$str ="<!-- Embed Scripts -->***СЮДА СЛОВО***<!-- End Embed -->"; 

Closed due to the fact that the issue is too general for participants Vladimir Martyanov , user194374, VenZell , Pavel Parshin , Grundy 13 Feb '16 at 21:52 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • between which lines? - Naumov
  • And where do you have a string in a variable or maybe just in the text of the program or maybe even in the database? - Mike
  • And is there already something standing there or is it empty and should I check the closing tag right after the opening tag in the original line? - Mike

1 answer 1

 $str ="<!-- Embed Scripts --><!-- End Embed -->"; print preg_replace('/<!-- Embed Scripts -->\K/','СЛОВО',$str); 

With a check for the presence of a closing tag (coming immediately after the opening tag):

 preg_replace('/<!-- Embed Scripts -->\K(?=<!-- End Embed -->)/','СЛОВО',$str); 

With the deletion of the previous content between the tags:

 preg_replace('/<!-- Embed Scripts -->\K.*?(?=<!-- End Embed -->)/s','СЛОВО',$str); 
  • And what if there is already a word? how to replace it? - Sdafs Fasafs
  • After \K in the second variant add .*? - Mike