Why do we have the dollar sign before every php variable?

Why won't this simple PHP code not work?

  • Regarding Preg_Replace function in PHP Greetings. I am trying to use the Preg_Replace function to replace a string variable with another string. This works fine when the string I'm looking for is written out, such as "This is my string." However, when I try to use the dollar sign to use a normal string, such as $This_is_my_string, I keep getting error messages about non-alphanumeric delimiters. I've tried every syntax I can think of, but I still get the error message: Below is an example of the sort of thing I'm trying so far without success: Anyone know how to fix this? Thanks, Brian $find = array ('$words_to_find'); $replace = array ('This is the Replacement String'); $Text = preg_replace ($find, $replace, $Text); I've also tried $find = ('/$words_to_find/'); and $find = ($words_to_find);

  • Answer:

    You are trying to replace a string, as such you should use str_replace instead. See: http://php.net/str_replace -----CodeStart----- <?php $string = '$Replace_This'; $withstring = 'This is the Replacement String'; echo str_replace($string, $withstring, $string); ?> -----CodeEnd----- Using an array: -----CodeStart----- <?php $string = '$Replace_This (In Between String) $Replace_This'; $Replace = array('$Replace_This'); $withstring = array('This is the Replacement String'); echo str_replace($Replace, $withstring, $string, $count) . ' '; echo 'Total Replaced variables: ' . $count; ?> -----CodeEnd----- If you for some reason still want to use preg_replace, the problem you where having was that the "$" sign is a special character, and as such it should be escaped with "\". Normally it would work simply to have the string inclosed by single quotes, but since the "Doller Sign" is used to design regular expressions with, we need to escape it. The below will work: -----CodeStart----- <?php $string = '$Replace_This (In Between String) $Replace_This'; $Replace = array('/\$Replace_This/'); $withstring = array('This is the Replacement String'); echo preg_replace($Replace, $withstring, $string) . ' '; ?> -----CodeEnd----- Note, the count parameter is optional, but can be used to return the number of matched patterns, which where replaced. If you are trying to replace usercodes with html equlivants, the following answer may interest you: http://answers.yahoo.com/question/index;_ylt=ApCGeScxN3tbEJl_lHeNzcHty6IX;_ylv=3?qid=20080312181143AAD2oa5&show=7#profile-info-83f19a0528c5c127d942b72c966f472daa

canterbu... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.