Preg_Replace Output Outside of Replacement String Missing


I believe/hope there is a simple fix. The preg_replace() is replacing all between and correctly. But the string “” is missing from the end area after calling preg_replace(). In other words, after preg_replace(), the portion located outside of and after and search/replace delimeters is also gone, unintentionally.

How do I preserve and display and future codings of this form in the final $str1 echo? Below is my code, actual output and desired output.



<?php $str1='START TEXT END TEXT'; $pattern = '#(.+?)#s'; $str1 = preg_replace($pattern, htmlentities(' ') ,$str1); echo $str1; ?>

Actual Output:


Desired Output:



I just tried your code and received your desired output… it seem to work just fine. Are you viewing output as HTML source?


Was the “” output also just before END TEXT?

Yes, here is exact output by your script:

[code]START TEXT <meta name="robots" content="index,follow" />
<meta name="description" content="NEW Worda wordb wordc" />

<meta name="keywords" content="NEW Wordd worde." />
<link rel="canonical" href="" /> END TEXT[/code]

I have tested with PHP 5.2 (Win/Apache)

I’m on Php 5.2 viewing a .php file with both firefox and IE browsers. I don’t know why the difference with your and my output. Are there any related server config settings?

Note that my output does not output the &quot notations but the actual symbols < >. I thought the htmlentities() forced output of actual symbols during the replacement. Does this help?

Are you viewing results as HTML source or just as HTML page? You need to view output as html source to see html comments .

I was so caught up in standard browser output that I forgot to check the actual source. I just knew this was to be a simple answer! Problem solved.

Thank You Much !

Sponsor our Newsletter | Privacy Policy | Terms of Service